home *** CD-ROM | disk | FTP | other *** search
/ Amiga Games Extra 1996 September / Amiga Games Extra CD-ROM 9-1996.iso / userbox / publicdomain / vim-4.2 / src / option.c < prev    next >
C/C++ Source or Header  |  1996-06-12  |  103KB  |  4,165 lines

  1. /* vi:set ts=4 sw=4:
  2.  *
  3.  * VIM - Vi IMproved        by Bram Moolenaar
  4.  *
  5.  * Do ":help uganda"  in Vim to read copying and usage conditions.
  6.  * Do ":help credits" in Vim to see a list of people who contributed.
  7.  */
  8.  
  9. /*
  10.  * Code to handle user-settable options. This is all pretty much table-
  11.  * driven. To add a new option, put it in the options array, and add a
  12.  * variable for it in option.h. If it's a numeric option, add any necessary
  13.  * bounds checks to do_set().
  14.  */
  15.  
  16. #include "vim.h"
  17. #include "globals.h"
  18. #include "proto.h"
  19. #include "option.h"
  20.  
  21. struct option
  22. {
  23.     char        *fullname;        /* full option name */
  24.     char        *shortname;     /* permissible abbreviation */
  25.     short         flags;            /* see below */
  26.     char_u        *var;            /* pointer to variable */
  27.     char_u        *def_val;        /* default value for variable (can be the same
  28.                                     as the actual value) */
  29. };
  30.  
  31. /*
  32.  * Flags
  33.  *
  34.  * Note: P_EXPAND and P_IND can never be used at the same time.
  35.  * Note: P_IND cannot be used for a terminal option.
  36.  */
  37. #define P_BOOL            0x01    /* the option is boolean */
  38. #define P_NUM            0x02    /* the option is numeric */
  39. #define P_STRING        0x04    /* the option is a string */
  40. #define P_ALLOCED        0x08    /* the string option is in allocated memory,
  41.                                     must use vim_free() when assigning new
  42.                                     value. Not set if default is the same. */
  43. #define P_EXPAND        0x10    /* environment expansion */
  44. #define P_IND            0x20    /* indirect, is in curwin or curbuf */
  45. #define P_NODEFAULT        0x40    /* has no default value */
  46. #define P_DEF_ALLOCED    0x80    /* default value is in allocated memory, must
  47.                                     use vim_free() when assigning new value */
  48. #define P_WAS_SET        0x100    /* option has been set/reset */
  49. #define P_NO_MKRC        0x200    /* don't include in :mkvimrc output */
  50.  
  51. /*
  52.  * The options that are in curwin or curbuf have P_IND set and a var field
  53.  * that contains one of the values below.
  54.  */
  55. #define PV_LIST        1
  56. #define PV_NU        2
  57. #define PV_SCROLL    3
  58. #define PV_WRAP        4
  59. #define PV_LBR        5
  60.  
  61. #define PV_AI        6
  62. #define PV_BIN        7
  63. #define PV_CIN        8
  64. #define PV_CINK        9
  65. #define PV_CINO        10
  66. #define PV_CINW        11
  67. #define PV_COM        12
  68. #define PV_EOL        13
  69. #define PV_ET        14
  70. #define PV_FO        15
  71. #define PV_LISP        16
  72. #define PV_ML        17
  73. #define PV_MOD        18
  74. #define PV_RO        20
  75. #define PV_SI        21
  76. #define PV_SN        22
  77. #define PV_SW        23
  78. #define PV_TS        24
  79. #define PV_TW        25
  80. #define PV_TX        26
  81. #define PV_WM        27
  82. #define PV_ISK        28
  83. #define PV_INF        29
  84. #define PV_RL        30
  85.  
  86. /*
  87.  * The option structure is initialized here.
  88.  * The order of the options should be alphabetic for ":set all".
  89.  * The options with a NULL variable are 'hidden': a set command for
  90.  * them is ignored and they are not printed.
  91.  */
  92. static struct option options[] =
  93. {
  94. #ifdef RIGHTLEFT
  95.     {"aleph",        "al",    P_NUM,                (char_u *)&p_aleph,
  96. # if defined(MSDOS) || defined(WIN32) || defined(OS2)
  97.                             (char_u *)128L},
  98. # else
  99.                             (char_u *)224L},
  100. # endif
  101. #endif
  102.     {"autoindent",    "ai",    P_BOOL|P_IND,        (char_u *)PV_AI,
  103.                             (char_u *)FALSE},
  104.     {"autoprint",    "ap",    P_BOOL,                (char_u *)NULL,
  105.                             (char_u *)FALSE},
  106.     {"autowrite",    "aw",    P_BOOL,                (char_u *)&p_aw,
  107.                             (char_u *)FALSE},
  108.     {"backspace",    "bs",    P_NUM,                (char_u *)&p_bs,
  109.                             (char_u *)0L},
  110.     {"backup",        "bk",    P_BOOL,                (char_u *)&p_bk,
  111.                             (char_u *)FALSE},
  112.     {"backupdir",    "bdir",    P_STRING|P_EXPAND,
  113.                                                 (char_u *)&p_bdir,
  114.                             (char_u *)DEF_BDIR},
  115.     {"backupext",    "bex",    P_STRING,            (char_u *)&p_bex,
  116. #ifdef VMS
  117.                             (char_u *)"_"},
  118. #else
  119.                             (char_u *)"~"},
  120. #endif
  121.     {"beautify",    "bf",    P_BOOL,                (char_u *)NULL,
  122.                             (char_u *)FALSE},
  123.     {"binary",        "bin",    P_BOOL|P_IND,        (char_u *)PV_BIN,
  124.                             (char_u *)FALSE},
  125.     {"bioskey",        "biosk",P_BOOL,
  126. #ifdef MSDOS
  127.                                                 (char_u *)&p_biosk,
  128. #else
  129.                                                 (char_u *)NULL,
  130. #endif
  131.                             (char_u *)TRUE},
  132.     {"breakat",        "brk",    P_STRING,            (char_u *)&p_breakat,
  133.                             (char_u *)" \t!@*-+_;:,./?"},
  134. #ifdef CINDENT
  135.     {"cindent",        "cin",  P_BOOL|P_IND,        (char_u *)PV_CIN,
  136.                             (char_u *)FALSE},
  137.     {"cinkeys",        "cink",    P_STRING|P_IND|P_ALLOCED,    (char_u *)PV_CINK,
  138.                             (char_u *)"0{,0},:,0#,!^F,o,O,e"},
  139.     {"cinoptions",    "cino", P_STRING|P_IND|P_ALLOCED,    (char_u *)PV_CINO,
  140.                             (char_u *)""},
  141. #endif /* CINDENT */
  142. #if defined(SMARTINDENT) || defined(CINDENT)
  143.     {"cinwords",    "cinw",    P_STRING|P_IND|P_ALLOCED,    (char_u *)PV_CINW,
  144.                             (char_u *)"if,else,while,do,for,switch"},
  145. #endif
  146.     {"cmdheight",    "ch",    P_NUM,                (char_u *)&p_ch,
  147.                             (char_u *)1L},
  148.     {"columns",        "co",    P_NUM|P_NODEFAULT|P_NO_MKRC, (char_u *)&Columns,
  149.                             (char_u *)80L},
  150.     {"comments",    "com",    P_STRING|P_IND|P_ALLOCED,    (char_u *)PV_COM,
  151.                             (char_u *)"sr:/*,mb:*,el:*/,://,b:#,:%,:XCOMM,n:>,fb:-"},
  152.     {"compatible",    "cp",    P_BOOL,                (char_u *)&p_cp,
  153.                             (char_u *)FALSE},
  154.     {"cpoptions",    "cpo",    P_STRING,            (char_u *)&p_cpo,
  155. #ifdef COMPATIBLE
  156.                             (char_u *)CPO_ALL},
  157. #else
  158.                             (char_u *)CPO_DEFAULT},
  159. #endif
  160.     {"define",        "def",    P_STRING,            (char_u *)&p_def,
  161.                             (char_u *)"^#[ \\t]*define"},
  162.     {"dictionary",    "dict",    P_STRING|P_EXPAND,    (char_u *)&p_dict,
  163.                             (char_u *)""},
  164.     {"digraph",        "dg",    P_BOOL,
  165. #ifdef DIGRAPHS
  166.                                                 (char_u *)&p_dg,
  167. #else
  168.                                                 (char_u *)NULL,
  169. #endif /* DIGRAPHS */
  170.                             (char_u *)FALSE},
  171.     {"directory",    "dir",    P_STRING|P_EXPAND,    (char_u *)&p_dir,
  172.                             (char_u *)DEF_DIR},
  173.     {"edcompatible","ed",    P_BOOL,                (char_u *)&p_ed,
  174.                             (char_u *)FALSE},
  175.     {"endofline",    "eol",    P_BOOL|P_IND|P_NO_MKRC,    (char_u *)PV_EOL,
  176.                             (char_u *)FALSE},
  177.     {"equalalways",    "ea",      P_BOOL,                (char_u *)&p_ea,
  178.                             (char_u *)TRUE},
  179.     {"equalprg",    "ep",      P_STRING|P_EXPAND,    (char_u *)&p_ep,
  180.                             (char_u *)""},
  181.     {"errorbells",    "eb",    P_BOOL,                (char_u *)&p_eb,
  182.                             (char_u *)FALSE},
  183.     {"errorfile",    "ef",      P_STRING|P_EXPAND,    (char_u *)&p_ef,
  184. #ifdef AMIGA
  185.                             (char_u *)"AztecC.Err"},
  186. #else
  187.                             (char_u *)"errors.vim"},
  188. #endif
  189.     {"errorformat",    "efm",     P_STRING,            (char_u *)&p_efm,
  190. #ifdef AMIGA
  191.                         /* don't use [^0-9] here, Manx C can't handle it */
  192.                             (char_u *)"%f>%l:%c:%t:%n:%m,%f:%l: %t%*[^0123456789]%n: %m,%f %l %t%*[^0123456789]%n: %m,%*[^\"]\"%f\"%*[^0123456789]%l: %m,%f:%l:%m"},
  193. #else
  194. # if defined MSDOS  ||  defined WIN32
  195.                             (char_u *)"%*[^\"]\"%f\"%*[^0-9]%l: %m,%f(%l) : %m,%*[^ ] %f %l: %m,%f:%l:%m"},
  196. # elif defined(__EMX__)    /* put most common here (i.e. gcc format) at front */
  197.                             (char_u *)"%f:%l:%m,%*[^\"]\"%f\"%*[^0-9]%l: %m,\"%f\"%*[^0-9]%l: %m"},
  198. # else
  199.                             (char_u *)"%*[^\"]\"%f\"%*[^0-9]%l: %m,\"%f\"%*[^0-9]%l: %m,%f:%l:%m"},
  200. # endif
  201. #endif
  202.     {"esckeys",        "ek",    P_BOOL,                (char_u *)&p_ek,
  203. #ifdef COMPATIBLE
  204.                             (char_u *)FALSE},
  205. #else
  206.                             (char_u *)TRUE},
  207. #endif
  208.     {"expandtab",    "et",    P_BOOL|P_IND,        (char_u *)PV_ET,
  209.                             (char_u *)FALSE},
  210.     {"exrc",        NULL,    P_BOOL,                (char_u *)&p_exrc,
  211.                             (char_u *)FALSE},
  212.     {"flash",        "fl",    P_BOOL,                (char_u *)NULL,
  213.                             (char_u *)FALSE},
  214.     {"formatoptions","fo",    P_STRING|P_IND|P_ALLOCED,    (char_u *)PV_FO,
  215. #ifdef COMPATIBLE
  216.                             (char_u *)FO_DFLT_VI},
  217. #else
  218.                             (char_u *)FO_DFLT},
  219. #endif
  220.     {"formatprg",    "fp",      P_STRING|P_EXPAND,    (char_u *)&p_fp,
  221.                             (char_u *)""},
  222.     {"gdefault",    "gd",    P_BOOL,                (char_u *)&p_gd,
  223.                             (char_u *)FALSE},
  224.     {"graphic",        "gr",    P_BOOL,                (char_u *)NULL,
  225.                             (char_u *)FALSE},
  226.     {"guifont",        "gfn",     P_STRING,
  227. #ifdef USE_GUI
  228.                                                 (char_u *)&p_guifont,
  229.                             (char_u *)""},
  230. #else
  231.                                                 (char_u *)NULL,
  232.                             (char_u *)NULL},
  233. #endif
  234.     {"guioptions",    "go",     P_STRING,
  235. #ifdef USE_GUI
  236.                                                 (char_u *)&p_guioptions,
  237. # ifdef UNIX
  238.                             (char_u *)"aAgmr"},
  239. # else
  240.                             (char_u *)"Agmr"},
  241. # endif
  242. #else
  243.                                                 (char_u *)NULL,
  244.                             (char_u *)NULL},
  245. #endif
  246. #if defined(USE_GUI)
  247.     {"guipty",        NULL,    P_BOOL,                (char_u *)&p_guipty,
  248.                             (char_u *)FALSE},
  249. #endif
  250.     {"hardtabs",    "ht",    P_NUM,                (char_u *)NULL,
  251.                             (char_u *)0L},
  252.     {"helpfile",    "hf",      P_STRING|P_EXPAND,    (char_u *)&p_hf,
  253.                             (char_u *)""},
  254.     {"helpheight",    "hh",      P_NUM,                (char_u *)&p_hh,
  255.                             (char_u *)20L},
  256.     {"hidden",        "hid",    P_BOOL,                (char_u *)&p_hid,
  257.                             (char_u *)FALSE},
  258.     {"highlight",    "hl",    P_STRING,            (char_u *)&p_hl,
  259.                             (char_u *)"8b,db,es,hs,mb,Mn,nu,rs,sr,tb,vr,ws"},
  260.     {"history",        "hi",     P_NUM,                (char_u *)&p_hi,
  261. #ifdef COMPATIBLE
  262.                             (char_u *)0L},
  263. #else
  264.                             (char_u *)20L},
  265. #endif
  266. #ifdef RIGHTLEFT
  267.     {"hkmap",        "hk",    P_BOOL,                (char_u *)&p_hkmap,
  268.                             (char_u *)FALSE},
  269. #endif
  270.     {"icon",         NULL,    P_BOOL,                (char_u *)&p_icon,
  271.                             (char_u *)FALSE},
  272.     {"ignorecase",    "ic",    P_BOOL,                (char_u *)&p_ic,
  273.                             (char_u *)FALSE},
  274.     {"include",        "inc",    P_STRING,            (char_u *)&p_inc,
  275.                             (char_u *)"^#[ \\t]*include"},
  276.     {"incsearch",    "is",    P_BOOL,                (char_u *)&p_is,
  277.                             (char_u *)FALSE},
  278.     {"infercase",    "inf",    P_BOOL|P_IND,        (char_u *)PV_INF,
  279.                             (char_u *)FALSE},
  280.     {"insertmode",    "im",    P_BOOL,                (char_u *)&p_im,
  281.                             (char_u *)FALSE},
  282.     {"isfname",        "isf",    P_STRING,            (char_u *)&p_isf,
  283. #ifdef BACKSLASH_IN_FILENAME
  284.                             (char_u *)"@,48-57,/,.,-,_,+,,,$,:,\\"},
  285. #else
  286. # ifdef AMIGA
  287.                             (char_u *)"@,48-57,/,.,-,_,+,,,$,:"},
  288. # else /* UNIX */
  289.                             (char_u *)"@,48-57,/,.,-,_,+,,,$,:,~"},
  290. # endif
  291. #endif
  292.     {"isident",        "isi",    P_STRING,            (char_u *)&p_isi,
  293. #if defined(MSDOS) || defined(WIN32) || defined(OS2)
  294.                             (char_u *)"@,48-57,_,128-167,224-235"},
  295. #else
  296.                             (char_u *)"@,48-57,_,192-255"},
  297. #endif
  298.     {"iskeyword",    "isk",    P_STRING|P_IND|P_ALLOCED,    (char_u *)PV_ISK,
  299. #ifdef COMPATIBLE
  300.                             (char_u *)"@,48-57,_"},
  301. #else
  302. # if defined MSDOS  ||  defined WIN32
  303.                             (char_u *)"@,48-57,_,128-167,224-235"},
  304. # else
  305.                             (char_u *)"@,48-57,_,192-255"},
  306. # endif
  307. #endif
  308.     {"isprint",        "isp",    P_STRING,            (char_u *)&p_isp,
  309. #if defined MSDOS  ||  defined WIN32
  310.                             (char_u *)"@,~-255"},
  311. #else
  312.                             (char_u *)"@,161-255"},
  313. #endif
  314.     {"joinspaces",    "js",    P_BOOL,                (char_u *)&p_js,
  315.                             (char_u *)TRUE},
  316.     {"keywordprg",    "kp",      P_STRING|P_EXPAND,    (char_u *)&p_kp,
  317. #if defined(MSDOS) ||  defined(WIN32)
  318.                             (char_u *)""},
  319. #else
  320.                             (char_u *)"man"},
  321. #endif
  322.     {"langmap",     "lmap", P_STRING,          
  323. #ifdef HAVE_LANGMAP
  324.                                                 (char_u *)&p_langmap,
  325.                             (char_u *)""},
  326. #else
  327.                                                 (char_u *)NULL,
  328.                             (char_u *)NULL},
  329. #endif
  330.     {"laststatus",    "ls",     P_NUM,                (char_u *)&p_ls,
  331.                             (char_u *)1L},
  332.     {"linebreak",    "lbr",    P_BOOL|P_IND,        (char_u *)PV_LBR,
  333.                             (char_u *)FALSE},
  334.     {"lines",        NULL,     P_NUM|P_NODEFAULT|P_NO_MKRC, (char_u *)&Rows,
  335. #if defined MSDOS  ||  defined WIN32
  336.                             (char_u *)25L},
  337. #else
  338.                             (char_u *)24L},
  339. #endif
  340.     {"lisp",        NULL,    P_BOOL|P_IND,        (char_u *)PV_LISP,
  341.                             (char_u *)FALSE},
  342.     {"list",        NULL,    P_BOOL|P_IND,        (char_u *)PV_LIST,
  343.                             (char_u *)FALSE},
  344.     {"magic",        NULL,    P_BOOL,                (char_u *)&p_magic,
  345.                             (char_u *)TRUE},
  346.     {"makeprg",        "mp",      P_STRING|P_EXPAND,    (char_u *)&p_mp,
  347.                             (char_u *)"make"},
  348.     {"maxmapdepth",    "mmd",    P_NUM,                (char_u *)&p_mmd,
  349.                             (char_u *)1000L},
  350.     {"maxmem",        "mm",    P_NUM,                (char_u *)&p_mm,
  351.                             (char_u *)MAXMEM},
  352.     {"maxmemtot",    "mmt",    P_NUM,                (char_u *)&p_mmt,
  353.                             (char_u *)MAXMEMTOT},
  354.     {"mesg",        NULL,    P_BOOL,                (char_u *)NULL,
  355.                             (char_u *)FALSE},
  356.     {"modeline",    "ml",    P_BOOL|P_IND,        (char_u *)PV_ML,
  357. #ifdef COMPATIBLE
  358.                             (char_u *)FALSE},
  359. #else
  360.                             (char_u *)TRUE},
  361. #endif
  362.     {"modelines",    "mls",    P_NUM,                (char_u *)&p_mls,
  363.                             (char_u *)5L},
  364.     {"modified",    "mod",    P_BOOL|P_IND|P_NO_MKRC,    (char_u *)PV_MOD,
  365.                             (char_u *)FALSE},
  366.     {"more",        NULL,    P_BOOL,                (char_u *)&p_more,
  367. #ifdef COMPATIBLE
  368.                             (char_u *)FALSE},
  369. #else
  370.                             (char_u *)TRUE},
  371. #endif
  372.     {"mouse",        NULL,    P_STRING,            (char_u *)&p_mouse,
  373. #if defined(MSDOS) || defined(WIN32)
  374.                             (char_u *)"a"},
  375. #else
  376.                             (char_u *)""},
  377. #endif
  378.     {"mousetime",    "mouset",    P_NUM,            (char_u *)&p_mouset,
  379.                             (char_u *)500L},
  380.     {"novice",        NULL,    P_BOOL,                (char_u *)NULL,
  381.                             (char_u *)FALSE},
  382.     {"number",        "nu",    P_BOOL|P_IND,        (char_u *)PV_NU,
  383.                             (char_u *)FALSE},
  384.     {"open",        NULL,    P_BOOL,                (char_u *)NULL,
  385.                             (char_u *)FALSE},
  386.     {"optimize",    "opt",    P_BOOL,                (char_u *)NULL,
  387.                             (char_u *)FALSE},
  388.     {"paragraphs",    "para",    P_STRING,            (char_u *)&p_para,
  389.                             (char_u *)"IPLPPPQPP LIpplpipbp"},
  390.     {"paste",        NULL,    P_BOOL,                (char_u *)&p_paste,
  391.                             (char_u *)FALSE},
  392.     {"patchmode",    "pm",   P_STRING,            (char_u *)&p_pm,
  393.                             (char_u *)""},
  394.     {"path",        "pa",      P_STRING|P_EXPAND,    (char_u *)&p_path,
  395. #if defined AMIGA  ||  defined MSDOS  ||  defined WIN32
  396.                             (char_u *)".,,"},
  397. #elif defined(__EMX__)
  398.                             (char_u *)".,/emx/include,,"},
  399. #else
  400.                             (char_u *)".,/usr/include,,"},
  401. #endif
  402.     {"prompt",        NULL,    P_BOOL,                (char_u *)NULL,
  403.                             (char_u *)FALSE},
  404.     {"readonly",    "ro",    P_BOOL|P_IND,        (char_u *)PV_RO,
  405.                             (char_u *)FALSE},
  406.     {"redraw",        NULL,    P_BOOL,                (char_u *)NULL,
  407.                             (char_u *)FALSE},
  408.     {"remap",        NULL,    P_BOOL,                (char_u *)&p_remap,
  409.                             (char_u *)TRUE},
  410.     {"report",        NULL,    P_NUM,                (char_u *)&p_report,
  411.                             (char_u *)2L},
  412. #ifdef WIN32
  413.     {"restorescreen", "rs",    P_BOOL,                (char_u *)&p_rs,
  414.                             (char_u *)TRUE},
  415. #endif
  416. #ifdef RIGHTLEFT
  417.     {"revins",        "ri",    P_BOOL,                (char_u *)&p_ri,
  418.                             (char_u *)FALSE},
  419.     {"rightleft",    "rl",    P_BOOL|P_IND,        (char_u *)PV_RL,
  420.                             (char_u *)FALSE},
  421. #endif
  422.     {"ruler",        "ru",    P_BOOL,                (char_u *)&p_ru,
  423.                             (char_u *)FALSE},
  424.     {"scroll",        "scr",     P_NUM|P_IND|P_NO_MKRC, (char_u *)PV_SCROLL,
  425.                             (char_u *)12L},
  426.     {"scrolljump",    "sj",     P_NUM,                (char_u *)&p_sj,
  427.                             (char_u *)1L},
  428.     {"scrolloff",    "so",     P_NUM,                (char_u *)&p_so,
  429.                             (char_u *)0L},
  430.     {"sections",    "sect",    P_STRING,            (char_u *)&p_sections,
  431.                             (char_u *)"SHNHH HUnhsh"},
  432.     {"secure",        NULL,    P_BOOL,                (char_u *)&p_secure,
  433.                             (char_u *)FALSE},
  434.     {"shell",        "sh",    P_STRING|P_EXPAND,    (char_u *)&p_sh,
  435. #if   defined(MSDOS)
  436.                             (char_u *)"command"},
  437. #elif defined(WIN32)
  438.                             (char_u *)""},        /* set in set_init_1() */
  439. #elif defined(__EMX__)
  440.                             (char_u *)"cmd.exe"},
  441. #elif defined(ARCHIE)
  442.                             (char_u *)"gos"},
  443. #else
  444.                             (char_u *)"sh"},
  445. #endif
  446.     {"shellpipe",    "sp",    P_STRING,            (char_u *)&p_sp,
  447. #if defined(UNIX) || defined(OS2)
  448. # ifdef ARCHIE
  449.                             (char_u *)"2>"},
  450. # else
  451.                             (char_u *)"| tee"},
  452. # endif
  453. #else
  454.                             (char_u *)">"},
  455. #endif
  456.     {"shellredir",    "srr",    P_STRING,            (char_u *)&p_srr,
  457.                             (char_u *)">"},
  458.     {"shelltype",    "st",    P_NUM,                (char_u *)&p_st,
  459.                             (char_u *)0L},
  460.     {"shiftround",    "sr",    P_BOOL,                (char_u *)&p_sr,
  461.                             (char_u *)FALSE},
  462.     {"shiftwidth",    "sw",    P_NUM|P_IND,        (char_u *)PV_SW,
  463.                             (char_u *)8L},
  464.     {"shortmess",    "shm",    P_STRING,            (char_u *)&p_shm,
  465.                             (char_u *)""},
  466.     {"shortname",    "sn",    P_BOOL|P_IND,
  467. #ifdef SHORT_FNAME
  468.                                                 (char_u *)NULL,
  469. #else
  470.                                                 (char_u *)PV_SN,
  471. #endif
  472.                             (char_u *)FALSE},
  473.     {"showbreak",    "sbr",    P_STRING,            (char_u *)&p_sbr,
  474.                             (char_u *)""},
  475.     {"showcmd",        "sc",    P_BOOL,                (char_u *)&p_sc,
  476. #if defined(COMPATIBLE) || defined(UNIX)
  477.                             (char_u *)FALSE},
  478. #else
  479.                             (char_u *)TRUE},
  480. #endif
  481.     {"showmatch",    "sm",    P_BOOL,                (char_u *)&p_sm,
  482.                             (char_u *)FALSE},
  483.     {"showmode",    "smd",    P_BOOL,                (char_u *)&p_smd,
  484. #if defined(COMPATIBLE)
  485.                             (char_u *)FALSE},
  486. #else
  487.                             (char_u *)TRUE},
  488. #endif
  489.     {"sidescroll",    "ss",    P_NUM,                (char_u *)&p_ss,
  490.                             (char_u *)0L},
  491.     {"slowopen",    "slow",    P_BOOL,                (char_u *)NULL,
  492.                             (char_u *)FALSE},
  493.     {"smartcase",    "scs",    P_BOOL,                (char_u *)&p_scs,
  494.                             (char_u *)FALSE},
  495. #ifdef SMARTINDENT
  496.     {"smartindent",    "si",    P_BOOL|P_IND,        (char_u *)PV_SI,
  497.                             (char_u *)FALSE},
  498. #endif
  499.     {"smarttab",    "sta",    P_BOOL,                (char_u *)&p_sta,
  500.                             (char_u *)FALSE},
  501.     {"sourceany",    NULL,    P_BOOL,                (char_u *)NULL,
  502.                             (char_u *)FALSE},
  503.     {"splitbelow",    "sb",    P_BOOL,                (char_u *)&p_sb,
  504.                             (char_u *)FALSE},
  505.     {"startofline",    "sol",    P_BOOL,                (char_u *)&p_sol,
  506.                             (char_u *)TRUE},
  507.     {"suffixes",    "su",    P_STRING,            (char_u *)&p_su,
  508.                             (char_u *)".bak,~,.o,.h,.info,.swp"},
  509.     {"swapsync",    "sws",    P_STRING,            (char_u *)&p_sws,
  510.                             (char_u *)"fsync"},
  511.     {"tabstop",        "ts",    P_NUM|P_IND,        (char_u *)PV_TS,
  512.                             (char_u *)8L},
  513.     {"taglength",    "tl",    P_NUM,                (char_u *)&p_tl,
  514.                             (char_u *)0L},
  515.     {"tagrelative",    "tr",    P_BOOL,                (char_u *)&p_tr,
  516. #if defined(COMPATIBLE)
  517.                             (char_u *)FALSE},
  518. #else
  519.                             (char_u *)TRUE},
  520. #endif
  521.     {"tags",        "tag",    P_STRING|P_EXPAND,    (char_u *)&p_tags,
  522. #ifdef EMACS_TAGS
  523.                             (char_u *)"./tags,./TAGS,tags,TAGS"},
  524. #else
  525.                             (char_u *)"./tags,tags"},
  526. #endif
  527.     {"tagstack",    "tgst",    P_BOOL,                (char_u *)NULL,
  528.                             (char_u *)FALSE},
  529.     {"term",        NULL,    P_STRING|P_EXPAND|P_NODEFAULT|P_NO_MKRC,
  530.                                             (char_u *)&term_strings[KS_NAME],
  531.                             (char_u *)""},
  532.     {"terse",        NULL,    P_BOOL,                (char_u *)&p_terse,
  533.                             (char_u *)FALSE},
  534.     {"textauto",    "ta",    P_BOOL,                (char_u *)&p_ta,
  535. #if defined(COMPATIBLE)
  536.                             (char_u *)FALSE},
  537. #else
  538.                             (char_u *)TRUE},
  539. #endif
  540.     {"textmode",    "tx",    P_BOOL|P_IND,        (char_u *)PV_TX,
  541. #ifdef USE_CRNL
  542.                             (char_u *)TRUE},
  543. #else
  544.                             (char_u *)FALSE},
  545. #endif
  546.     {"textwidth",    "tw",    P_NUM|P_IND,        (char_u *)PV_TW,
  547.                             (char_u *)0L},
  548.     {"tildeop",        "top",    P_BOOL,                (char_u *)&p_to,
  549.                             (char_u *)FALSE},
  550.     {"timeout",     "to",    P_BOOL,                (char_u *)&p_timeout,
  551.                             (char_u *)TRUE},
  552.     {"timeoutlen",    "tm",    P_NUM,                (char_u *)&p_tm,
  553.                             (char_u *)1000L},
  554.     {"title",         NULL,    P_BOOL,                (char_u *)&p_title,
  555.                             (char_u *)FALSE},
  556.     {"ttimeout",     NULL,    P_BOOL,                (char_u *)&p_ttimeout,
  557.                             (char_u *)FALSE},
  558.     {"ttimeoutlen",    "ttm",    P_NUM,                (char_u *)&p_ttm,
  559.                             (char_u *)-1L},
  560.     {"ttybuiltin",    "tbi",    P_BOOL,                (char_u *)&p_tbi,
  561.                             (char_u *)TRUE},
  562.     {"ttyfast",        "tf",    P_BOOL|P_NO_MKRC,    (char_u *)&p_tf,
  563.                             (char_u *)FALSE},
  564.     {"ttyscroll",    "tsl",    P_NUM,                (char_u *)&p_ttyscroll,
  565.                             (char_u *)999L},
  566.     {"ttytype",        "tty",    P_STRING|P_EXPAND|P_NODEFAULT|P_NO_MKRC,
  567.                                             (char_u *)&term_strings[KS_NAME],
  568.                             (char_u *)""},
  569.     {"undolevels",    "ul",    P_NUM,                (char_u *)&p_ul,
  570. #ifdef COMPATIBLE
  571.                             (char_u *)0L},
  572. #else
  573. # if defined(UNIX) || defined(WIN32) || defined(OS2)
  574.                             (char_u *)1000L},
  575. # else
  576.                             (char_u *)100L},
  577. # endif
  578. #endif
  579.     {"updatecount",    "uc",    P_NUM,                (char_u *)&p_uc,
  580. #ifdef COMPATIBLE
  581.                             (char_u *)0L},
  582. #else
  583.                             (char_u *)200L},
  584. #endif
  585.     {"updatetime",    "ut",    P_NUM,                (char_u *)&p_ut,
  586.                             (char_u *)4000L},
  587.     {"viminfo",        "vi",    P_STRING,
  588. #ifdef VIMINFO
  589.                                                 (char_u *)&p_viminfo,
  590. #else
  591.                                                 (char_u *)NULL,
  592. #endif /* VIMINFO */
  593.                             (char_u *)""},
  594.     {"visualbell",    "vb",    P_BOOL,                (char_u *)&p_vb,
  595.                             (char_u *)FALSE},
  596.     {"w300",        NULL,     P_NUM,                (char_u *)NULL,
  597.                             (char_u *)0L},
  598.     {"w1200",        NULL,     P_NUM,                (char_u *)NULL,
  599.                             (char_u *)0L},
  600.     {"w9600",        NULL,     P_NUM,                (char_u *)NULL,
  601.                             (char_u *)0L},
  602.     {"warn",        NULL,    P_BOOL,                (char_u *)&p_warn,
  603.                             (char_u *)TRUE},
  604.     {"weirdinvert",    "wiv",    P_BOOL,                (char_u *)&p_wiv,
  605.                             (char_u *)FALSE},
  606.     {"whichwrap",    "ww",    P_STRING,            (char_u *)&p_ww,
  607. #ifdef COMPATIBLE
  608.                             (char_u *)""},
  609. #else
  610.                             (char_u *)"b,s"},
  611. #endif
  612.     {"wildchar",    "wc",     P_NUM,                (char_u *)&p_wc,
  613. #ifdef COMPATIBLE
  614.                             (char_u *)(long)Ctrl('E')},
  615. #else
  616.                             (char_u *)(long)TAB},
  617. #endif
  618.     {"window",        "wi",     P_NUM,                (char_u *)NULL,
  619.                             (char_u *)0L},
  620.     {"winheight",    "wh",    P_NUM,                (char_u *)&p_wh,
  621.                             (char_u *)0L},
  622.     {"wrap",        NULL,    P_BOOL|P_IND,        (char_u *)PV_WRAP,
  623.                             (char_u *)TRUE},
  624.     {"wrapmargin",    "wm",    P_NUM|P_IND,        (char_u *)PV_WM,
  625.                             (char_u *)0L},
  626.     {"wrapscan",    "ws",    P_BOOL,                (char_u *)&p_ws,
  627.                             (char_u *)TRUE},
  628.     {"writeany",    "wa",    P_BOOL,                (char_u *)&p_wa,
  629.                             (char_u *)FALSE},
  630.     {"writebackup",    "wb",    P_BOOL,                (char_u *)&p_wb,
  631. #if defined(COMPATIBLE) && !defined(WRITEBACKUP)
  632.                             (char_u *)FALSE},
  633. #else
  634.                             (char_u *)TRUE},
  635. #endif
  636.     {"writedelay",    "wd",    P_NUM,                (char_u *)&p_wd,
  637.                             (char_u *)0L},
  638.  
  639. /* terminal output codes */
  640.     {"t_AL",        NULL,    P_STRING,    (char_u *)&term_strings[KS_CAL],
  641.                             (char_u *)""},
  642.     {"t_al",        NULL,    P_STRING,    (char_u *)&term_strings[KS_AL],
  643.                             (char_u *)""},
  644.     {"t_cd",        NULL,    P_STRING,    (char_u *)&term_strings[KS_CD],
  645.                             (char_u *)""},
  646.     {"t_ce",        NULL,    P_STRING,    (char_u *)&term_strings[KS_CE],
  647.                             (char_u *)""},
  648.     {"t_cl",        NULL,    P_STRING,    (char_u *)&term_strings[KS_CL],
  649.                             (char_u *)""},
  650.     {"t_cm",        NULL,    P_STRING,    (char_u *)&term_strings[KS_CM],
  651.                             (char_u *)""},
  652.     {"t_CS",        NULL,    P_STRING,    (char_u *)&term_strings[KS_CSC],
  653.                             (char_u *)""},
  654.     {"t_cs",        NULL,    P_STRING,    (char_u *)&term_strings[KS_CS],
  655.                             (char_u *)""},
  656.     {"t_da",        NULL,    P_STRING,    (char_u *)&term_strings[KS_DA],
  657.                             (char_u *)""},
  658.     {"t_db",        NULL,    P_STRING,    (char_u *)&term_strings[KS_DB],
  659.                             (char_u *)""},
  660.     {"t_DL",        NULL,    P_STRING,    (char_u *)&term_strings[KS_CDL],
  661.                             (char_u *)""},
  662.     {"t_dl",        NULL,    P_STRING,    (char_u *)&term_strings[KS_DL],
  663.                             (char_u *)""},
  664.     {"t_ke",        NULL,    P_STRING,    (char_u *)&term_strings[KS_KE],
  665.                             (char_u *)""},
  666.     {"t_ks",        NULL,    P_STRING,    (char_u *)&term_strings[KS_KS],
  667.                             (char_u *)""},
  668.     {"t_md",        NULL,    P_STRING,    (char_u *)&term_strings[KS_MD],
  669.                             (char_u *)""},
  670.     {"t_me",        NULL,    P_STRING,    (char_u *)&term_strings[KS_ME],
  671.                             (char_u *)""},
  672.     {"t_mr",        NULL,    P_STRING,    (char_u *)&term_strings[KS_MR],
  673.                             (char_u *)""},
  674.     {"t_ms",        NULL,    P_STRING,    (char_u *)&term_strings[KS_MS],
  675.                             (char_u *)""},
  676.     {"t_RI",        NULL,    P_STRING,    (char_u *)&term_strings[KS_CRI],
  677.                             (char_u *)""},
  678.     {"t_se",        NULL,    P_STRING,    (char_u *)&term_strings[KS_SE],
  679.                             (char_u *)""},
  680.     {"t_so",        NULL,    P_STRING,    (char_u *)&term_strings[KS_SO],
  681.                             (char_u *)""},
  682.     {"t_sr",        NULL,    P_STRING,    (char_u *)&term_strings[KS_SR],
  683.                             (char_u *)""},
  684.     {"t_te",        NULL,    P_STRING,    (char_u *)&term_strings[KS_TE],
  685.                             (char_u *)""},
  686.     {"t_ti",        NULL,    P_STRING,    (char_u *)&term_strings[KS_TI],
  687.                             (char_u *)""},
  688.     {"t_ue",        NULL,    P_STRING,    (char_u *)&term_strings[KS_UE],
  689.                             (char_u *)""},
  690.     {"t_us",        NULL,    P_STRING,    (char_u *)&term_strings[KS_US],
  691.                             (char_u *)""},
  692.     {"t_vb",        NULL,    P_STRING,    (char_u *)&term_strings[KS_VB],
  693.                             (char_u *)""},
  694.     {"t_ve",        NULL,    P_STRING,    (char_u *)&term_strings[KS_VE],
  695.                             (char_u *)""},
  696.     {"t_vi",        NULL,    P_STRING,    (char_u *)&term_strings[KS_VI],
  697.                             (char_u *)""},
  698.     {"t_vs",        NULL,    P_STRING,    (char_u *)&term_strings[KS_VS],
  699.                             (char_u *)""},
  700.     {"t_ZH",        NULL,    P_STRING,    (char_u *)&term_strings[KS_CZH],
  701.                             (char_u *)""},
  702.     {"t_ZR",        NULL,    P_STRING,    (char_u *)&term_strings[KS_CZR],
  703.                             (char_u *)""},
  704.  
  705. /* terminal key codes are not here */
  706.  
  707.     {NULL, NULL, 0, NULL, NULL}            /* end marker */
  708. };
  709.  
  710. #define PARAM_COUNT (sizeof(options) / sizeof(struct option))
  711.  
  712. #ifdef AUTOCMD
  713. /*
  714.  * structures for automatic commands
  715.  */
  716.  
  717. typedef struct AutoCmd
  718. {
  719.     char_u            *cmd;                    /* The command to be executed */
  720.     struct AutoCmd    *next;                    /* Next AutoCmd in list */
  721. } AutoCmd;
  722.  
  723. typedef struct AutoPat
  724. {
  725.     char_u            *pat;                    /* pattern as typed */
  726.     char_u            *reg_pat;                /* pattern converted to regexp */
  727.     int                allow_directories;        /* Pattern may match whole path */
  728.     AutoCmd            *cmds;                    /* list of commands to do */
  729.     struct AutoPat    *next;                    /* next AutoPat in AutoPat list */
  730. } AutoPat;
  731.  
  732. static struct event_name
  733. {
  734.     char    *name;        /* event name */
  735.     int        event;        /* event number */
  736. } event_names[] =
  737. {
  738.     {"BufEnter",        EVENT_BUFENTER},
  739.     {"BufLeave",        EVENT_BUFLEAVE},
  740.     {"BufNewFile",        EVENT_BUFNEWFILE},
  741.     {"BufReadPost",        EVENT_BUFREADPOST},
  742.     {"BufReadPre",        EVENT_BUFREADPRE},
  743.     {"BufRead",         EVENT_BUFREADPOST},
  744.     {"BufWritePost",     EVENT_BUFWRITEPOST},
  745.     {"BufWritePre",        EVENT_BUFWRITEPRE},
  746.     {"BufWrite",        EVENT_BUFWRITEPRE},
  747.     {"FileAppendPost",    EVENT_FILEAPPENDPOST},
  748.     {"FileAppendPre",    EVENT_FILEAPPENDPRE},
  749.     {"FileReadPost",    EVENT_FILEREADPOST},
  750.     {"FileReadPre",        EVENT_FILEREADPRE},
  751.     {"FileWritePost",    EVENT_FILEWRITEPOST},
  752.     {"FileWritePre",    EVENT_FILEWRITEPRE},
  753.     {"FilterReadPost",    EVENT_FILTERREADPOST},
  754.     {"FilterReadPre",    EVENT_FILTERREADPRE},
  755.     {"FilterWritePost",    EVENT_FILTERWRITEPOST},
  756.     {"FilterWritePre",    EVENT_FILTERWRITEPRE},
  757.     {"VimLeave",        EVENT_VIMLEAVE},
  758.     {"WinEnter",        EVENT_WINENTER},
  759.     {"WinLeave",        EVENT_WINLEAVE},
  760.     {NULL,            0}
  761. };
  762.  
  763. static AutoPat *first_autopat[NUM_EVENTS] =
  764. {
  765.     NULL, NULL, NULL, NULL, NULL,
  766.     NULL, NULL, NULL, NULL, NULL,
  767.     NULL, NULL, NULL, NULL, NULL,
  768.     NULL, NULL, NULL, NULL, NULL
  769. };
  770. #endif
  771.  
  772. static void set_option_default __ARGS((int, int));
  773. static void illegal_char __ARGS((char_u *, int));
  774. static char_u *option_expand __ARGS((int));
  775. static int findoption __ARGS((char_u *));
  776. static int find_key_option __ARGS((char_u *));
  777. static void    showoptions __ARGS((int));
  778. static int option_changed __ARGS((struct option *));
  779. static void showoneopt __ARGS((struct option *));
  780. static int  istermoption __ARGS((struct option *));
  781. static char_u *get_varp __ARGS((struct option *));
  782. static void option_value2string __ARGS((struct option *));
  783. #ifdef HAVE_LANGMAP
  784. static void langmap_init __ARGS((void));
  785. static void langmap_set __ARGS((void));
  786. #endif
  787. static void paste_option_changed __ARGS((void));
  788. static void p_compatible_set __ARGS((void));
  789. static void fill_breakat_flags __ARGS((void));
  790.  
  791. /*
  792.  * Initialize the options, first part.
  793.  *
  794.  * Called only once from main(), just after creating the first buffer.
  795.  */
  796.     void
  797. set_init_1()
  798. {
  799.     char_u    *p;
  800.     int        opt_idx;
  801.     long    n;
  802.  
  803. #ifdef HAVE_LANGMAP
  804.     langmap_init();
  805. #endif
  806.  
  807. /*
  808.  * Find default value for 'shell' option.
  809.  */
  810.     if ((p = vim_getenv((char_u *)"SHELL")) != NULL
  811. #if defined(MSDOS) || defined(WIN32) || defined(OS2)
  812. # ifdef __EMX__
  813.             || (p = vim_getenv((char_u *)"EMXSHELL")) != NULL
  814. # endif
  815.             || (p = vim_getenv((char_u *)"COMSPEC")) != NULL
  816. # ifdef WIN32
  817.             || (p = default_shell()) != NULL
  818. # endif
  819. #endif
  820.                                                             )
  821.     {
  822.         p = strsave(p);
  823.         if (p != NULL)            /* we don't want a NULL */
  824.         {
  825.             opt_idx = findoption((char_u *)"sh");
  826.             options[opt_idx].def_val = p;
  827.             options[opt_idx].flags |= P_DEF_ALLOCED;
  828.         }
  829.     }
  830.  
  831. /*
  832.  * Set default for 'helpfile' option. This cannot be done at compile time,
  833.  * because for Unix it is an external variable.
  834.  */
  835.     opt_idx = findoption((char_u *)"hf");
  836. #if defined(HAVE_CONFIG_H) || defined(OS2)
  837.     options[opt_idx].def_val = help_fname;
  838. #else
  839.     options[opt_idx].def_val = (char_u *)VIM_HLP;
  840. #endif
  841.  
  842. /*
  843.  * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory
  844.  */
  845.     opt_idx = findoption((char_u *)"maxmemtot");
  846.     if (options[opt_idx].def_val == (char_u *)0L)
  847.     {
  848.         n = (mch_avail_mem(FALSE) >> 11);
  849.         options[opt_idx].def_val = (char_u *)n;
  850.         opt_idx = findoption((char_u *)"maxmem");
  851.         if ((long)options[opt_idx].def_val > n ||
  852.                                          (long)options[opt_idx].def_val == 0L)
  853.             options[opt_idx].def_val = (char_u *)n;
  854.     }
  855.  
  856. /*
  857.  * set all the options (except the terminal options) to their default value
  858.  */
  859.     for (opt_idx = 0; !istermoption(&options[opt_idx]); opt_idx++)
  860.         if (!(options[opt_idx].flags & P_NODEFAULT))
  861.             set_option_default(opt_idx, FALSE);
  862.  
  863.     curbuf->b_p_initialized = TRUE;
  864.     check_buf_options(curbuf);
  865.     check_options();
  866.  
  867.     /*
  868.      * initialize the table for 'iskeyword' et.al.
  869.      * Must be before option_expand(), because that one needs isidchar()
  870.      */
  871.     init_chartab();
  872.  
  873.     /*
  874.      * initialize the table for 'breakat'.
  875.      */
  876.     fill_breakat_flags();
  877.  
  878.     /*
  879.      * Expand environment variables and things like "~" for the defaults.
  880.      * If option_expand() returns non-NULL the variable is expanded. This can
  881.      * only happen for non-indirect options.
  882.      * Also set the default to the expanded value, so ":set" does not list
  883.      * them. Don't set the P_ALLOCED flag, because we don't want to free the
  884.      * default.
  885.      */
  886.     for (opt_idx = 0; !istermoption(&options[opt_idx]); opt_idx++)
  887.     {
  888.         p = option_expand(opt_idx);
  889.         if (p != NULL)
  890.         {
  891.             *(char_u **)options[opt_idx].var = p;
  892.             options[opt_idx].def_val = p;
  893.             options[opt_idx].flags |= P_DEF_ALLOCED;
  894.         }
  895.     }
  896. }
  897.  
  898. /*
  899.  * Set an option to its default value.
  900.  */
  901.     static void
  902. set_option_default(opt_idx, dofree)
  903.     int        opt_idx;
  904.     int        dofree;        /* TRUE when old value may be freed */
  905. {
  906.     char_u        *varp;            /* pointer to variable for current option */
  907.  
  908.     varp = get_varp(&(options[opt_idx]));
  909.     if (varp != NULL)        /* nothing to do for hidden option */
  910.     {
  911.         if (options[opt_idx].flags & P_STRING)
  912.         {
  913.             /* indirect options are always in allocated memory */
  914.             if (options[opt_idx].flags & P_IND)
  915.                 set_string_option(NULL, opt_idx,
  916.                                             options[opt_idx].def_val, dofree);
  917.             else
  918.             {
  919.                 if (dofree && (options[opt_idx].flags & P_ALLOCED))
  920.                     free_string_option(*(char_u **)(varp));
  921.                 *(char_u **)varp = options[opt_idx].def_val;
  922.                 options[opt_idx].flags &= ~P_ALLOCED;
  923.             }
  924.         }
  925.         else if (options[opt_idx].flags & P_NUM)
  926.             *(long *)varp = (long)options[opt_idx].def_val;
  927.         else    /* P_BOOL */
  928.                 /* the cast to long is required for Manx C */
  929.             *(int *)varp = (int)(long)options[opt_idx].def_val;
  930.     }
  931. }
  932.  
  933. /*
  934.  * Initialize the options, part two: After getting Rows and Columns
  935.  */
  936.     void
  937. set_init_2()
  938. {
  939. /*
  940.  * 'scroll' defaults to half the window height. Note that this default is
  941.  * wrong when the window height changes.
  942.  */
  943.     options[findoption((char_u *)"scroll")].def_val = (char_u *)(Rows >> 1);
  944.  
  945.     comp_col();
  946. }
  947.  
  948. /*
  949.  * Initialize the options, part three: After reading the .vimrc
  950.  */
  951.     void
  952. set_init_3()
  953. {
  954.     int        idx1;
  955.  
  956. #if defined(UNIX) || defined(OS2)
  957. /*
  958.  * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
  959.  * This is done after other initializations, where 'shell' might have been
  960.  * set, but only if they have not been set before.
  961.  */
  962.     char_u    *p;
  963.     int        idx2;
  964.     int        do_sp;
  965.     int        do_srr;
  966.  
  967.     idx1 = findoption((char_u *)"sp");
  968.     idx2 = findoption((char_u *)"srr");
  969.     do_sp = !(options[idx1].flags & P_WAS_SET);
  970.     do_srr = !(options[idx2].flags & P_WAS_SET);
  971.  
  972.     /*
  973.      * Default for p_sp is "| tee", for p_srr is ">".
  974.      * For known shells it is changed here to include stderr.
  975.      */
  976.     p = gettail(p_sh);
  977.     if (    fnamecmp(p, "csh") == 0 ||
  978.             fnamecmp(p, "tcsh") == 0
  979. # ifdef OS2            /* also check with .exe extension */
  980.             || fnamecmp(p, "csh.exe") == 0
  981.             || fnamecmp(p, "tcsh.exe") == 0
  982. # endif
  983.                                             )
  984.     {
  985.         if (do_sp)
  986.         {
  987.             p_sp = (char_u *)"|& tee";
  988.             options[idx1].def_val = p_sp;
  989.         }
  990.         if (do_srr)
  991.         {
  992.             p_srr = (char_u *)">&";
  993.             options[idx2].def_val = p_srr;
  994.         }
  995.     }
  996.     else
  997. # ifndef OS2    /* Always use bourne shell style redirection if we reach this */
  998.         if (    STRCMP(p, "sh") == 0 ||
  999.                 STRCMP(p, "ksh") == 0 ||
  1000.                 STRCMP(p, "zsh") == 0 ||
  1001.                 STRCMP(p, "bash") == 0)
  1002. # endif
  1003.     {
  1004.         if (do_sp)
  1005.         {
  1006.             p_sp = (char_u *)"2>&1| tee";
  1007.             options[idx1].def_val = p_sp;
  1008.         }
  1009.         if (do_srr)
  1010.         {
  1011.             p_srr = (char_u *)">%s 2>&1";
  1012.             options[idx2].def_val = p_srr;
  1013.         }
  1014.     }
  1015. #endif
  1016.  
  1017. /*
  1018.  * 'title' and 'icon' only default to true if they have not been set or reset
  1019.  * in .vimrc and we can read the old value.
  1020.  * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
  1021.  * they can be reset. this reduces startup time when using X on a remote
  1022.  * machine.
  1023.  */
  1024.     idx1 = findoption((char_u *)"title");
  1025.     if (!(options[idx1].flags & P_WAS_SET) && mch_can_restore_title())
  1026.     {
  1027.         options[idx1].def_val = (char_u *)TRUE;
  1028.         p_title = TRUE;
  1029.     }
  1030.     idx1 = findoption((char_u *)"icon");
  1031.     if (!(options[idx1].flags & P_WAS_SET) && mch_can_restore_icon())
  1032.     {
  1033.         options[idx1].def_val = (char_u *)TRUE;
  1034.         p_icon = TRUE;
  1035.     }
  1036. }
  1037.  
  1038. /*
  1039.  * Parse 'arg' for option settings.
  1040.  *
  1041.  * 'arg' may be IObuff, but only when no errors can be present and option
  1042.  * does not need to be expanded with option_expand().
  1043.  *
  1044.  * return FAIL if errors are detected, OK otherwise
  1045.  */
  1046.     int
  1047. do_set(arg)
  1048.     char_u        *arg;    /* option string (may be written to!) */
  1049. {
  1050.     register int opt_idx;
  1051.     char_u        *errmsg;
  1052.     char_u        errbuf[80];
  1053.     char_u        *startarg;
  1054.     int            prefix;    /* 1: nothing, 0: "no", 2: "inv" in front of name */
  1055.     int         nextchar;            /* next non-white char after option name */
  1056.     int            afterchar;            /* character just after option name */
  1057.     int         len;
  1058.     int            i;
  1059.     int            key;
  1060.     int         flags;                /* flags for current option */
  1061.     char_u        *varp = NULL;        /* pointer to variable for current option */
  1062.     char_u        *oldval;            /* previous value if *varp */
  1063.     int            errcnt = 0;            /* number of errornous entries */
  1064.     long        oldRows = Rows;        /* remember old Rows */
  1065.     long        oldColumns = Columns;    /* remember old Columns */
  1066.     int            oldbin;                /* remember old bin option */
  1067.     long        oldch = p_ch;        /* remember old command line height */
  1068.     int            oldea = p_ea;        /* remember old 'equalalways' */
  1069.     long        olduc = p_uc;        /* remember old 'updatecount' */
  1070.     int            did_show = FALSE;    /* already showed one value */
  1071.     WIN            *wp;
  1072.  
  1073.     if (*arg == NUL)
  1074.     {
  1075.         showoptions(0);
  1076.         return OK;
  1077.     }
  1078.  
  1079.     while (*arg)        /* loop to process all options */
  1080.     {
  1081.         errmsg = NULL;
  1082.         startarg = arg;        /* remember for error message */
  1083.  
  1084.         if (STRNCMP(arg, "all", (size_t)3) == 0)
  1085.         {
  1086.             showoptions(1);
  1087.             arg += 3;
  1088.         }
  1089.         else if (STRNCMP(arg, "termcap", (size_t)7) == 0)
  1090.         {
  1091.             showoptions(2);
  1092.             show_termcodes();
  1093.             arg += 7;
  1094.         }
  1095.         else
  1096.         {
  1097.             prefix = 1;
  1098.             if (STRNCMP(arg, "no", (size_t)2) == 0)
  1099.             {
  1100.                 prefix = 0;
  1101.                 arg += 2;
  1102.             }
  1103.             else if (STRNCMP(arg, "inv", (size_t)3) == 0)
  1104.             {
  1105.                 prefix = 2;
  1106.                 arg += 3;
  1107.             }
  1108.                 /* find end of name */
  1109.             if (*arg == '<')
  1110.             {
  1111.                 opt_idx = -1;
  1112.                 /* check for <t_>;> */
  1113.                 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
  1114.                     len = 5;
  1115.                 else
  1116.                 {
  1117.                     len = 1;
  1118.                     while (arg[len] != NUL && arg[len] != '>')
  1119.                         ++len;
  1120.                 }
  1121.                 if (arg[len] != '>')
  1122.                 {
  1123.                     errmsg = e_invarg;
  1124.                     goto skip;
  1125.                 }
  1126.                 nextchar = arg[len];
  1127.                 arg[len] = NUL;                        /* put NUL after name */
  1128.                 if (arg[1] == 't' && arg[2] == '_')    /* could be term code */
  1129.                     opt_idx = findoption(arg + 1);
  1130.                 key = 0;
  1131.                 if (opt_idx == -1)
  1132.                     key = find_key_option(arg + 1);
  1133.                 arg[len++] = nextchar;                /* restore nextchar */
  1134.                 nextchar = arg[len];
  1135.             }
  1136.             else
  1137.             {
  1138.                 len = 0;
  1139.                 /*
  1140.                  * The two characters after "t_" may not be alphanumeric.
  1141.                  */
  1142.                 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
  1143.                 {
  1144.                     len = 4;
  1145.                 }
  1146.                 else
  1147.                 {
  1148.                     while (isalnum(arg[len]) || arg[len] == '_')
  1149.                         ++len;
  1150.                 }
  1151.                 nextchar = arg[len];
  1152.                 arg[len] = NUL;                        /* put NUL after name */
  1153.                 opt_idx = findoption(arg);
  1154.                 key = 0;
  1155.                 if (opt_idx == -1)
  1156.                     key = find_key_option(arg);
  1157.                 arg[len] = nextchar;                /* restore nextchar */
  1158.             }
  1159.  
  1160.             if (opt_idx == -1 && key == 0)        /* found a mismatch: skip */
  1161.             {
  1162.                 errmsg = (char_u *)"Unknown option";
  1163.                 goto skip;
  1164.             }
  1165.  
  1166.             if (opt_idx >= 0)
  1167.             {
  1168.                 if (options[opt_idx].var == NULL)    /* hidden option: skip */
  1169.                     goto skip;
  1170.  
  1171.                 flags = options[opt_idx].flags;
  1172.                 varp = get_varp(&(options[opt_idx]));
  1173.             }
  1174.             else
  1175.                 flags = P_STRING;
  1176.  
  1177.             /* remember character after option name */
  1178.             afterchar = nextchar;
  1179.  
  1180.             /* skip white space, allow ":set ai  ?" */
  1181.             while (vim_iswhite(nextchar))
  1182.                 nextchar = arg[++len];
  1183.  
  1184.             if (vim_strchr((char_u *)"?=:!&", nextchar) != NULL)
  1185.             {
  1186.                 arg += len;
  1187.                 len = 0;
  1188.             }
  1189.  
  1190.             /*
  1191.              * allow '=' and ':' as MSDOS command.com allows only one
  1192.              * '=' character per "set" command line. grrr. (jw)
  1193.              */
  1194.             if (nextchar == '?' || (prefix == 1 && vim_strchr((char_u *)"=:&",
  1195.                                       nextchar) == NULL && !(flags & P_BOOL)))
  1196.             {                                        /* print value */
  1197.                 if (did_show)
  1198.                     msg_outchar('\n');        /* cursor below last one */
  1199.                 else
  1200.                 {
  1201.                     gotocmdline(TRUE);        /* cursor at status line */
  1202.                     did_show = TRUE;        /* remember that we did a line */
  1203.                 }
  1204.                 if (opt_idx >= 0)
  1205.                     showoneopt(&options[opt_idx]);
  1206.                 else
  1207.                 {
  1208.                     char_u            name[2];
  1209.                     char_u            *p;
  1210.  
  1211.                     name[0] = KEY2TERMCAP0(key);
  1212.                     name[1] = KEY2TERMCAP1(key);
  1213.                     p = find_termcode(name);
  1214.                     if (p == NULL)
  1215.                     {
  1216.                         errmsg = (char_u *)"Unknown option";
  1217.                         goto skip;
  1218.                     }
  1219.                     else
  1220.                         (void)show_one_termcode(name, p, TRUE);
  1221.                 }
  1222.                 if (nextchar != '?' && nextchar != NUL &&
  1223.                                                       !vim_iswhite(afterchar))
  1224.                     errmsg = e_trailing;
  1225.             }
  1226.             else
  1227.             {
  1228.                 if (flags & P_BOOL)                    /* boolean */
  1229.                 {
  1230.                     if (nextchar == '=' || nextchar == ':')
  1231.                     {
  1232.                         errmsg = e_invarg;
  1233.                         goto skip;
  1234.                     }
  1235.  
  1236.                     /*
  1237.                      * in secure mode, setting of the secure option is not
  1238.                      * allowed
  1239.                      */
  1240.                     if (secure && (int *)varp == &p_secure)
  1241.                     {
  1242.                         errmsg = (char_u *)"not allowed here";
  1243.                         goto skip;
  1244.                     }
  1245.  
  1246.                     oldbin = curbuf->b_p_bin;    /* remember old bin option */
  1247.  
  1248.                     /*
  1249.                      * ":set opt!" or ":set invopt": invert
  1250.                      * ":set opt&": reset to default value
  1251.                      * ":set opt" or ":set noopt": set or reset
  1252.                      */
  1253.                     if (prefix == 2 || nextchar == '!')
  1254.                         *(int *)(varp) ^= 1;
  1255.                     else if (nextchar == '&')
  1256.                                 /* the cast to long is required for Manx C */
  1257.                         *(int *)(varp) = (int)(long)options[opt_idx].def_val;
  1258.                     else
  1259.                         *(int *)(varp) = prefix;
  1260.  
  1261.                     /* handle the setting of the compatible option */
  1262.                     if ((int *)varp == &p_cp && p_cp)
  1263.                     {
  1264.                         p_compatible_set();
  1265.                     }
  1266.                     /* when 'readonly' is reset, also reset readonlymode */
  1267.                     else if ((int *)varp == &curbuf->b_p_ro && !curbuf->b_p_ro)
  1268.                         readonlymode = FALSE;
  1269.  
  1270.                     /* when 'bin' is set also set some other options */
  1271.                     else if ((int *)varp == &curbuf->b_p_bin)
  1272.                     {
  1273.                         set_options_bin(oldbin, curbuf->b_p_bin);
  1274.                     }
  1275.                     /* when 'terse' is set change 'shortmess' */
  1276.                     else if ((int *)varp == &p_terse)
  1277.                     {
  1278.                         char_u    *p;
  1279.  
  1280.                         p = vim_strchr(p_shm, SHM_SEARCH);
  1281.  
  1282.                         /* insert 's' in p_shm */
  1283.                         if (p_terse && p == NULL)
  1284.                         {
  1285.                             STRCPY(IObuff, p_shm);
  1286.                             STRCAT(IObuff, "s");
  1287.                             set_string_option((char_u *)"shm", -1,
  1288.                                                                 IObuff, TRUE);
  1289.                         }
  1290.                         /* remove 's' from p_shm */
  1291.                         else if (!p_terse && p != NULL)
  1292.                             vim_memmove(p, p + 1, STRLEN(p));
  1293.                     }
  1294.                     /* when 'paste' is set or reset also change other options */
  1295.                     else if ((int *)varp == &p_paste)
  1296.                     {
  1297.                         paste_option_changed();
  1298.                     }
  1299.                     /*
  1300.                      * When 'lisp' option changes include/exclude '-' in
  1301.                      * keyword characters.
  1302.                      */
  1303.                     else if (varp == (char_u *)&(curbuf->b_p_lisp))
  1304.                         init_chartab();        /* ignore errors */
  1305.  
  1306.                     else if (!starting && ((int *)varp == &p_title ||
  1307.                                                       (int *)varp == &p_icon))
  1308.                     {
  1309.                         /*
  1310.                          * When setting 'title' or 'icon' on, call maketitle()
  1311.                          * to create and display it.
  1312.                          * When resetting 'title' or 'icon', call maketitle()
  1313.                          * to clear it and call mch_restore_title() to get the
  1314.                          * old value back.
  1315.                          */
  1316.                         maketitle();
  1317.                         if (!*(int *)varp)
  1318.                             mch_restore_title((int *)varp == &p_title ? 1 : 2);
  1319.                     }
  1320.                 }
  1321.                 else                                /* numeric or string */
  1322.                 {
  1323.                     if (vim_strchr((char_u *)"=:&", nextchar) == NULL ||
  1324.                                                                   prefix != 1)
  1325.                     {
  1326.                         errmsg = e_invarg;
  1327.                         goto skip;
  1328.                     }
  1329.                     if (flags & P_NUM)                /* numeric */
  1330.                     {
  1331.                         /*
  1332.                          * Different ways to set a number option:
  1333.                          * &        set to default value
  1334.                          * <xx>        accept special key codes for 'wildchar'
  1335.                          * c        accept any non-digit for 'wildchar'
  1336.                          * 0-9        set number
  1337.                          * other    error
  1338.                          */
  1339.                         arg += len + 1;
  1340.                         if (nextchar == '&')
  1341.                             *(long *)(varp) = (long)options[opt_idx].def_val;
  1342.                         else if ((long *)varp == &p_wc &&
  1343.                                                 (*arg == '<' || *arg == '^' ||
  1344.                                           ((!arg[1] || vim_iswhite(arg[1])) &&
  1345.                                                              !isdigit(*arg))))
  1346.                         {
  1347.                             if (*arg == '<')
  1348.                             {
  1349.                                 i = get_special_key_code(arg + 1);
  1350.                                 if (i == 0)
  1351.                                     i = find_key_option(arg + 1);
  1352.                             }
  1353.                             else if (*arg == '^')
  1354.                                 i = arg[1] ^ 0x40;
  1355.                             else
  1356.                                 i = *arg;
  1357.                             if (i == 0)
  1358.                             {
  1359.                                 errmsg = e_invarg;
  1360.                                 goto skip;
  1361.                             }
  1362.                             p_wc = i;
  1363.                         }
  1364.                                 /* allow negative numbers (for 'undolevels') */
  1365.                         else if (*arg == '-' || isdigit(*arg))
  1366.                         {
  1367.                             i = 0;
  1368.                             if (*arg == '-')
  1369.                                 i = 1;
  1370. #ifdef HAVE_STRTOL
  1371.                             *(long *)(varp) = strtol((char *)arg, NULL, 0);
  1372.                             if (arg[i] == '0' && TO_UPPER(arg[i + 1]) == 'X')
  1373.                                 i += 2;
  1374. #else
  1375.                             *(long *)(varp) = atol((char *)arg);
  1376. #endif
  1377.                             while (isdigit(arg[i]))
  1378.                                 ++i;
  1379.                             if (arg[i] != NUL && !vim_iswhite(arg[i]))
  1380.                             {
  1381.                                 errmsg = e_invarg;
  1382.                                 goto skip;
  1383.                             }
  1384.                         }
  1385.                         else
  1386.                         {
  1387.                             errmsg = (char_u *)"Number required after =";
  1388.                             goto skip;
  1389.                         }
  1390.  
  1391.                         /*
  1392.                          * Number options that need some action when changed
  1393.                          */
  1394.                         if ((long *)varp == &p_wh || (long *)varp == &p_hh)
  1395.                         {
  1396.                             if (p_wh < 0)
  1397.                             {
  1398.                                 errmsg = e_positive;
  1399.                                 p_wh = 0;
  1400.                             }
  1401.                             if (p_hh < 0)
  1402.                             {
  1403.                                 errmsg = e_positive;
  1404.                                 p_hh = 0;
  1405.                             }
  1406.                                 /* Change window height NOW */
  1407.                             if (p_wh && lastwin != firstwin)
  1408.                             {
  1409.                                 win_equal(curwin, FALSE);
  1410.                                 must_redraw = CLEAR;
  1411.                             }
  1412.                         }
  1413.                         /* (re)set last window status line */
  1414.                         if ((long *)varp == &p_ls)
  1415.                             last_status();
  1416.                     }
  1417.                     else if (opt_idx >= 0)                    /* string */
  1418.                     {
  1419.                         char_u        *save_arg = NULL;
  1420.                         char_u        *s, *p;
  1421.                         int            new_value_alloced;    /* new string option
  1422.                                                            was allocated */
  1423.  
  1424.                         /* The old value is kept until we are sure that the new
  1425.                          * value is valid. set_option_default() is therefore
  1426.                          * called with FALSE
  1427.                          */
  1428.                         oldval = *(char_u **)(varp);
  1429.                         if (nextchar == '&')        /* set to default val */
  1430.                         {
  1431.                             set_option_default(opt_idx, FALSE);
  1432.                             new_value_alloced =
  1433.                                          (options[opt_idx].flags & P_ALLOCED);
  1434.                         }
  1435.                         else
  1436.                         {
  1437.                             arg += len + 1;    /* jump to after the '=' or ':' */
  1438.  
  1439.                             /*
  1440.                              * Convert 'whichwrap' number to string, for
  1441.                              * backwards compatibility with Vim 3.0.
  1442.                              * Misuse errbuf[] for the resulting string.
  1443.                              */
  1444.                             if (varp == (char_u *)&p_ww && isdigit(*arg))
  1445.                             {
  1446.                                 *errbuf = NUL;
  1447.                                 i = getdigits(&arg);
  1448.                                 if (i & 1)
  1449.                                     STRCAT(errbuf, "b,");
  1450.                                 if (i & 2)
  1451.                                     STRCAT(errbuf, "s,");
  1452.                                 if (i & 4)
  1453.                                     STRCAT(errbuf, "h,l,");
  1454.                                 if (i & 8)
  1455.                                     STRCAT(errbuf, "<,>,");
  1456.                                 if (i & 16)
  1457.                                     STRCAT(errbuf, "[,],");
  1458.                                 if (*errbuf != NUL)        /* remove trailing , */
  1459.                                     errbuf[STRLEN(errbuf) - 1] = NUL;
  1460.                                 save_arg = arg;
  1461.                                 arg = errbuf;
  1462.                             }
  1463.                             /*
  1464.                              * Remove '>' before 'dir' and 'bdir', for
  1465.                              * backwards compatibility with version 3.0
  1466.                              */
  1467.                             else if (*arg == '>' && (varp == (char_u *)&p_dir ||
  1468.                                                    varp == (char_u *)&p_bdir))
  1469.                             {
  1470.                                 ++arg;
  1471.                             }
  1472.  
  1473.                             /*
  1474.                              * Copy the new string into allocated memory.
  1475.                              * Can't use set_string_option(), because we need
  1476.                              * to remove the backslashes.
  1477.                              */
  1478.                                                 /* get a bit too much */
  1479.                             s = alloc((unsigned)(STRLEN(arg) + 1));
  1480.                             if (s == NULL)    /* out of memory, don't change */
  1481.                                 break;
  1482.                             *(char_u **)(varp) = s;
  1483.  
  1484.                             /*
  1485.                              * Copy the string, skip over escaped chars.
  1486.                              * For MS-DOS and WIN32 backslashes before normal
  1487.                              * file name characters are not removed.
  1488.                              */
  1489.                             while (*arg && !vim_iswhite(*arg))
  1490.                             {
  1491.                                 if (*arg == '\\' && arg[1] != NUL
  1492. #ifdef BACKSLASH_IN_FILENAME
  1493.                                         && !((flags & P_EXPAND)
  1494.                                                 && isfilechar(arg[1])
  1495.                                                 && arg[1] != '\\')
  1496. #endif
  1497.                                                                     )
  1498.                                     ++arg;
  1499.                                 *s++ = *arg++;
  1500.                             }
  1501.                             *s = NUL;
  1502.                             if (save_arg != NULL)    /* number for 'whichwrap' */
  1503.                                 arg = save_arg;
  1504.                             new_value_alloced = TRUE;
  1505.                         }
  1506.  
  1507.                             /* expand environment variables and ~ */
  1508.                         s = option_expand(opt_idx);
  1509.                         if (s != NULL)
  1510.                         {
  1511.                             if (new_value_alloced)
  1512.                                 vim_free(*(char_u **)(varp));
  1513.                             *(char_u **)(varp) = s;
  1514.                             new_value_alloced = TRUE;
  1515.                         }
  1516.  
  1517.                         /*
  1518.                          * options that need some action
  1519.                          * to perform when changed (jw)
  1520.                          */
  1521.                         if (varp == (char_u *)&term_strings[KS_NAME])
  1522.                         {
  1523.                             if (term_strings[KS_NAME][0] == NUL)
  1524.                                 errmsg = (char_u *)"Cannot set 'term' to empty string";
  1525. #ifdef USE_GUI
  1526.                             if (gui.in_use)
  1527.                                 errmsg = (char_u *)"Cannot change term in GUI";
  1528. #endif
  1529.                             else if (set_termname(term_strings[KS_NAME]) ==
  1530.                                                                          FAIL)
  1531.                                 errmsg = (char_u *)"Not found in termcap";
  1532.                             else
  1533.                             {
  1534.                                 /* Screen colors may have changed. */
  1535.                                 outstr(T_ME);
  1536.                                 updateScreen(CLEAR);
  1537.                             }
  1538.                         }
  1539.  
  1540.                         else if ((varp == (char_u *)&p_bex ||
  1541.                                                      varp == (char_u *)&p_pm))
  1542.                         {
  1543.                             if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
  1544.                                          *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
  1545.                                 errmsg = (char_u *)"'backupext' and 'patchmode' are equal";
  1546.                         }
  1547.                         /*
  1548.                          * 'isident', 'iskeyword', 'isprint or 'isfname'
  1549.                          *  option: refill chartab[]
  1550.                          * If the new option is invalid, use old value.
  1551.                          * 'lisp' option: refill chartab[] for '-' char
  1552.                          */
  1553.                         else if (varp == (char_u *)&p_isi ||
  1554.                                  varp == (char_u *)&(curbuf->b_p_isk) ||
  1555.                                  varp == (char_u *)&p_isp ||
  1556.                                  varp == (char_u *)&p_isf)
  1557.                         {
  1558.                             if (init_chartab() == FAIL)
  1559.                                 errmsg = e_invarg;        /* error in value */
  1560.                         }
  1561.                         else if (varp == (char_u *)&p_hl)
  1562.                         {
  1563.                             /* Check 'highlight' */
  1564.                             for (s = p_hl; *s; )
  1565.                             {
  1566.                                 if (vim_strchr((char_u *)"8dehmMnrstvw",
  1567.                                                         (i = s[0])) == NULL ||
  1568.                                     vim_strchr((char_u *)"bsnuir",
  1569.                                                         (i = s[1])) == NULL ||
  1570.                                               ((i = s[2]) != NUL && i != ','))
  1571.                                 {
  1572.                                     illegal_char(errbuf, i);
  1573.                                     errmsg = errbuf;
  1574.                                     break;
  1575.                                 }
  1576.                                 if (s[2] == NUL)
  1577.                                     break;
  1578.                                 s = skipwhite(s + 3);
  1579.                             }
  1580.                         }
  1581.                         else if (varp == (char_u *)&(curbuf->b_p_com))
  1582.                         {
  1583.                             for (s = curbuf->b_p_com; *s; )
  1584.                             {
  1585.                                 while (*s && *s != ':')
  1586.                                 {
  1587.                                     if (vim_strchr((char_u *)COM_ALL, *s) == NULL)
  1588.                                     {
  1589.                                         errmsg = (char_u *)"Illegal flag";
  1590.                                         break;
  1591.                                     }
  1592.                                     ++s;
  1593.                                 }
  1594.                                 if (*s++ == NUL)
  1595.                                     errmsg = (char_u *)"Missing colon";
  1596.                                 else if (*s == ',')
  1597.                                     errmsg = (char_u *)"Zero length string";
  1598.                                 if (errmsg != NULL)
  1599.                                     break;
  1600.                                 while (*s && *s != ',')
  1601.                                 {
  1602.                                     if (*s == '\\' && s[1] != NUL)
  1603.                                         ++s;
  1604.                                     ++s;
  1605.                                 }
  1606.                                 s = skip_to_option_part(s);
  1607.                             }
  1608.                         }
  1609. #ifdef VIMINFO
  1610.                         else if (varp == (char_u *)&(p_viminfo))
  1611.                         {
  1612.                             for (s = p_viminfo; *s;)
  1613.                             {
  1614.                                 /* Check it's a valid character */
  1615.                                 if (vim_strchr((char_u *)"\"'fr:/", *s) == NULL)
  1616.                                 {
  1617.                                     illegal_char(errbuf, *s);
  1618.                                     errmsg = errbuf;
  1619.                                     break;
  1620.                                 }
  1621.                                 if (*s == 'r')
  1622.                                 {
  1623.                                     while (*++s && *s != ',')
  1624.                                         ;
  1625.                                 }
  1626.                                 else
  1627.                                 {
  1628.                                     while (isdigit(*++s))
  1629.                                         ;
  1630.  
  1631.                                     /* Must be a number after the character */
  1632.                                     if (!isdigit(*(s - 1)))
  1633.                                     {
  1634.                                         sprintf((char *)errbuf,
  1635.                                                 "Missing number after <%s>",
  1636.                                                 transchar(*(s - 1)));
  1637.                                         errmsg = errbuf;
  1638.                                         break;
  1639.                                     }
  1640.                                 }
  1641.                                 s = skip_to_option_part(s);
  1642.                             }
  1643.                             if (*p_viminfo && errmsg == NULL
  1644.                                             && get_viminfo_parameter('\'') < 0)
  1645.                                 errmsg = (char_u *)"Must specify a ' value";
  1646.                         }
  1647. #endif /* VIMINFO */
  1648.                         else if (istermoption(&options[opt_idx]) && full_screen)
  1649.                         {
  1650.                             ttest(FALSE);
  1651.                             if (varp == (char_u *)&term_strings[KS_ME])
  1652.                             {
  1653.                                 outstr(T_ME);
  1654.                                 updateScreen(CLEAR);
  1655.                             }
  1656.                         }
  1657.                         else if (varp == (char_u *)&p_sbr)
  1658.                         {
  1659.                             for (s = p_sbr; *s; ++s)
  1660.                                 if (charsize(*s) != 1)
  1661.                                     errmsg = (char_u *)"contains unprintable character";
  1662.                         }
  1663. #ifdef USE_GUI
  1664.                         else if (varp == (char_u *)&p_guifont)
  1665.                         {
  1666.                             gui_init_font();
  1667.                         }
  1668. #endif /* USE_GUI */
  1669. #ifdef HAVE_LANGMAP
  1670.                         else if (varp == (char_u *)&p_langmap)
  1671.                             langmap_set();
  1672. #endif
  1673.                         else if (varp == (char_u *)&p_breakat)
  1674.                             fill_breakat_flags();
  1675.                         else
  1676.                         {
  1677.                             /*
  1678.                              * Check options that are a list of flags.
  1679.                              */
  1680.                             p = NULL;
  1681.                             if (varp == (char_u *)&p_ww)
  1682.                                 p = (char_u *)WW_ALL;
  1683.                             if (varp == (char_u *)&p_shm)
  1684.                                 p = (char_u *)SHM_ALL;
  1685.                             else if (varp == (char_u *)&(p_cpo))
  1686.                                 p =    (char_u *)CPO_ALL;
  1687.                             else if (varp == (char_u *)&(curbuf->b_p_fo))
  1688.                                 p = (char_u *)FO_ALL;
  1689.                             else if (varp == (char_u *)&p_mouse)
  1690.                             {
  1691. #ifdef USE_MOUSE
  1692.                                 p = (char_u *)MOUSE_ALL;
  1693. #else
  1694.                                 if (*p_mouse != NUL)
  1695.                                     errmsg = (char_u *)"No mouse support";
  1696. #endif
  1697.                             }
  1698. #ifdef USE_GUI
  1699.                             else if (varp == (char_u *)&p_guioptions)
  1700.                                 p = (char_u *)GO_ALL;
  1701. #endif /* USE_GUI */
  1702.                             if (p != NULL)
  1703.                             {
  1704.                                 for (s = *(char_u **)(varp); *s; ++s)
  1705.                                     if (vim_strchr(p, *s) == NULL)
  1706.                                     {
  1707.                                         illegal_char(errbuf, *s);
  1708.                                         errmsg = errbuf;
  1709.                                         break;
  1710.                                     }
  1711.                             }
  1712.                         }
  1713.                         if (errmsg != NULL)    /* error detected */
  1714.                         {
  1715.                             if (new_value_alloced)
  1716.                                 vim_free(*(char_u **)(varp));
  1717.                             *(char_u **)(varp) = oldval;
  1718.                             (void)init_chartab();    /* back to the old value */
  1719.                             goto skip;
  1720.                         }
  1721.  
  1722. #ifdef USE_GUI
  1723.                         if (varp == (char_u *)&p_guioptions)
  1724.                             gui_init_which_components(oldval);
  1725. #endif /* USE_GUI */
  1726.  
  1727.                         /*
  1728.                          * Free string options that are in allocated memory.
  1729.                          */
  1730.                         if (flags & P_ALLOCED)
  1731.                             free_string_option(oldval);
  1732.                         if (new_value_alloced)
  1733.                             options[opt_idx].flags |= P_ALLOCED;
  1734.                     }
  1735.                     else            /* key code option */
  1736.                     {
  1737.                         char_u        name[2];
  1738.                         char_u        *p;
  1739.  
  1740.                         name[0] = KEY2TERMCAP0(key);
  1741.                         name[1] = KEY2TERMCAP1(key);
  1742.                         if (nextchar == '&')
  1743.                         {
  1744.                             if (add_termcap_entry(name, TRUE) == FAIL)
  1745.                                 errmsg = (char_u *)"Not found in termcap";
  1746.                         }
  1747.                         else
  1748.                         {
  1749.                             arg += len + 1;    /* jump to after the '=' or ':' */
  1750.                             for(p = arg; *p && !vim_iswhite(*p); ++p)
  1751.                             {
  1752.                                 if (*p == '\\' && *(p + 1))
  1753.                                     ++p;
  1754.                             }
  1755.                             nextchar = *p;
  1756.                             *p = NUL;
  1757.                             add_termcode(name, arg);
  1758.                             *p = nextchar;
  1759.                         }
  1760.                         if (full_screen)
  1761.                             ttest(FALSE);
  1762.                     }
  1763.                 }
  1764.                 if (opt_idx >= 0)
  1765.                     options[opt_idx].flags |= P_WAS_SET;
  1766.             }
  1767.  
  1768. skip:
  1769.             /*
  1770.              * Check the bounds for numeric options here
  1771.              */
  1772.             if (Rows < min_rows() && full_screen)
  1773.             {
  1774.                 sprintf((char *)errbuf, "Need at least %d lines", min_rows());
  1775.                 errmsg = errbuf;
  1776.                 Rows = min_rows();
  1777.             }
  1778.             if (Columns < MIN_COLUMNS && full_screen)
  1779.             {
  1780.                 sprintf((char *)errbuf, "Need at least %d columns",
  1781.                                                                  MIN_COLUMNS);
  1782.                 errmsg = errbuf;
  1783.                 Columns = MIN_COLUMNS;
  1784.             }
  1785.             /*
  1786.              * If the screenheight has been changed, assume it is the physical
  1787.              * screenheight.
  1788.              */
  1789.             if ((oldRows != Rows || oldColumns != Columns) && full_screen)
  1790.             {
  1791.                 mch_set_winsize();            /* try to change the window size */
  1792.                 check_winsize();            /* in case 'columns' changed */
  1793. #ifdef MSDOS
  1794.                 set_window();        /* active window may have changed */
  1795. #endif
  1796.             }
  1797.  
  1798.             if (curbuf->b_p_ts <= 0)
  1799.             {
  1800.                 errmsg = e_positive;
  1801.                 curbuf->b_p_ts = 8;
  1802.             }
  1803.             if (curbuf->b_p_tw < 0)
  1804.             {
  1805.                 errmsg = e_positive;
  1806.                 curbuf->b_p_tw = 0;
  1807.             }
  1808.             if (p_tm < 0)
  1809.             {
  1810.                 errmsg = e_positive;
  1811.                 p_tm = 0;
  1812.             }
  1813.             if ((curwin->w_p_scroll <= 0 ||
  1814.                         curwin->w_p_scroll > curwin->w_height) && full_screen)
  1815.             {
  1816.                 if (curwin->w_p_scroll != 0)
  1817.                     errmsg = e_scroll;
  1818.                 win_comp_scroll(curwin);
  1819.             }
  1820.             if (p_report < 0)
  1821.             {
  1822.                 errmsg = e_positive;
  1823.                 p_report = 1;
  1824.             }
  1825.             if ((p_sj < 0 || p_sj >= Rows) && full_screen)
  1826.             {
  1827.                 if (Rows != oldRows)        /* Rows changed, just adjust p_sj */
  1828.                     p_sj = Rows / 2;
  1829.                 else
  1830.                 {
  1831.                     errmsg = e_scroll;
  1832.                     p_sj = 1;
  1833.                 }
  1834.             }
  1835.             if (p_so < 0 && full_screen)
  1836.             {
  1837.                 errmsg = e_scroll;
  1838.                 p_so = 0;
  1839.             }
  1840.             if (p_uc < 0)
  1841.             {
  1842.                 errmsg = e_positive;
  1843.                 p_uc = 100;
  1844.             }
  1845.             if (p_ch < 1)
  1846.             {
  1847.                 errmsg = e_positive;
  1848.                 p_ch = 1;
  1849.             }
  1850.             if (p_ut < 0)
  1851.             {
  1852.                 errmsg = e_positive;
  1853.                 p_ut = 2000;
  1854.             }
  1855.             if (p_ss < 0)
  1856.             {
  1857.                 errmsg = e_positive;
  1858.                 p_ss = 0;
  1859.             }
  1860.  
  1861.             /*
  1862.              * Advance to next argument.
  1863.              * - skip until a blank found, taking care of backslashes
  1864.              * - skip blanks
  1865.              */
  1866.             while (*arg != NUL && !vim_iswhite(*arg))
  1867.                 if (*arg++ == '\\' && *arg != NUL)
  1868.                     ++arg;
  1869.         }
  1870.         arg = skipwhite(arg);
  1871.  
  1872.         if (errmsg)
  1873.         {
  1874.             ++no_wait_return;    /* wait_return done below */
  1875. #ifdef SLEEP_IN_EMSG
  1876.             ++dont_sleep;        /* don't wait in emsg() */
  1877. #endif
  1878.             emsg(errmsg);        /* show error highlighted */
  1879. #ifdef SLEEP_IN_EMSG
  1880.             --dont_sleep;
  1881. #endif
  1882.             MSG_OUTSTR(": ");
  1883.                                 /* show argument normal */
  1884.             while (startarg < arg)
  1885.                 msg_outstr(transchar(*startarg++));
  1886.             msg_end();            /* check for scrolling */
  1887.             --no_wait_return;
  1888.  
  1889.             ++errcnt;            /* count number of errors */
  1890.             did_show = TRUE;    /* error message counts as show */
  1891.             if (sourcing_name != NULL)
  1892.                 break;
  1893.         }
  1894.     }
  1895.  
  1896.     /*
  1897.      * when 'updatecount' changes from zero to non-zero, open swap files
  1898.      */
  1899.     if (p_uc && !olduc)
  1900.         ml_open_files();
  1901.  
  1902.     if (p_ch != oldch)                /* p_ch changed value */
  1903.         command_height();
  1904. #ifdef USE_MOUSE
  1905.     if (*p_mouse == NUL)
  1906.         mch_setmouse(FALSE);        /* switch mouse off */
  1907.     else
  1908.         setmouse();                    /* in case 'mouse' changed */
  1909. #endif
  1910.     comp_col();                        /* in case 'ruler' or 'showcmd' changed */
  1911.     curwin->w_set_curswant = TRUE;    /* in case 'list' changed */
  1912.  
  1913.     /*
  1914.      * Update the screen in case we changed something like "tabstop" or
  1915.      * "lines" or "list" that will change its appearance.
  1916.      * Also update the cursor position, in case 'wrap' is changed.
  1917.      */
  1918.     for (wp = firstwin; wp; wp = wp->w_next)
  1919.         wp->w_redr_status = TRUE;        /* mark all status lines dirty */
  1920.     if (p_ea && !oldea)
  1921.         win_equal(curwin, FALSE);
  1922.     updateScreen(CURSUPD);
  1923.     return (errcnt == 0 ? OK : FAIL);
  1924. }
  1925.  
  1926.     static void
  1927. illegal_char(errbuf, c)
  1928.     char_u        *errbuf;
  1929.     int            c;
  1930. {
  1931.     sprintf((char *)errbuf, "Illegal character <%s>", (char *)transchar(c));
  1932. }
  1933.  
  1934. /*
  1935.  * set_options_bin -  called when 'bin' changes value.
  1936.  */
  1937.     void
  1938. set_options_bin(oldval, newval)
  1939.     int        oldval;
  1940.     int        newval;
  1941. {
  1942.     /*
  1943.      * The option values that are changed when 'bin' changes are
  1944.      * copied when 'bin is set and restored when 'bin' is reset.
  1945.      */
  1946.     if (newval)
  1947.     {
  1948.         if (!oldval)            /* switched on */
  1949.         {
  1950.             curbuf->b_p_tw_nobin = curbuf->b_p_tw;
  1951.             curbuf->b_p_wm_nobin = curbuf->b_p_wm;
  1952.             curbuf->b_p_tx_nobin = curbuf->b_p_tx;
  1953.             curbuf->b_p_ta_nobin = p_ta;
  1954.             curbuf->b_p_ml_nobin = curbuf->b_p_ml;
  1955.             curbuf->b_p_et_nobin = curbuf->b_p_et;
  1956.         }
  1957.  
  1958.         curbuf->b_p_tw = 0;        /* no automatic line wrap */
  1959.         curbuf->b_p_wm = 0;        /* no automatic line wrap */
  1960.         curbuf->b_p_tx = 0;        /* no text mode */
  1961.         p_ta           = 0;        /* no text auto */
  1962.         curbuf->b_p_ml = 0;        /* no modelines */
  1963.         curbuf->b_p_et = 0;        /* no expandtab */
  1964.     }
  1965.     else if (oldval)            /* switched off */
  1966.     {
  1967.         curbuf->b_p_tw = curbuf->b_p_tw_nobin;
  1968.         curbuf->b_p_wm = curbuf->b_p_wm_nobin;
  1969.         curbuf->b_p_tx = curbuf->b_p_tx_nobin;
  1970.         p_ta           = curbuf->b_p_ta_nobin;
  1971.         curbuf->b_p_ml = curbuf->b_p_ml_nobin;
  1972.         curbuf->b_p_et = curbuf->b_p_et_nobin;
  1973.     }
  1974. }
  1975.  
  1976. #ifdef VIMINFO
  1977. /*
  1978.  * Find the parameter represented by the given character (eg ', :, ", or /),
  1979.  * and return its associated value in the 'viminfo' string.  If the parameter
  1980.  * is not specified in the string, return -1.
  1981.  */
  1982.     int
  1983. get_viminfo_parameter(type)
  1984.     int        type;
  1985. {
  1986.     char_u    *p;
  1987.  
  1988.     p = vim_strchr(p_viminfo, type);
  1989.     if (p != NULL && isdigit(*++p))
  1990.         return (int)atol((char *)p);
  1991.     return -1;
  1992. }
  1993. #endif
  1994.  
  1995. /*
  1996.  * Expand environment variables for some string options.
  1997.  * These string options cannot be indirect!
  1998.  * Return pointer to allocated memory, or NULL when not expanded.
  1999.  */
  2000.     static char_u *
  2001. option_expand(opt_idx)
  2002.     int        opt_idx;
  2003. {
  2004.     char_u        *p;
  2005.  
  2006.         /* if option doesn't need expansion or is hidden: nothing to do */
  2007.     if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
  2008.         return NULL;
  2009.  
  2010.     p = *(char_u **)(options[opt_idx].var);
  2011.  
  2012.     /*
  2013.      * Expanding this with NameBuff, expand_env() must not be passed IObuff.
  2014.      */
  2015.     expand_env(p, NameBuff, MAXPATHL);
  2016.     if (STRCMP(NameBuff, p) == 0)    /* they are the same */
  2017.         return NULL;
  2018.  
  2019.     return strsave(NameBuff);
  2020. }
  2021.  
  2022. /*
  2023.  * Check for string options that are NULL (normally only termcap options).
  2024.  */
  2025.     void
  2026. check_options()
  2027. {
  2028.     int        opt_idx;
  2029.     char_u    **p;
  2030.  
  2031.     for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
  2032.         if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
  2033.         {
  2034.             p = (char_u **)get_varp(&(options[opt_idx]));
  2035.             if (*p == NULL)
  2036.                 *p = empty_option;
  2037.         }
  2038. }
  2039.  
  2040. /*
  2041.  * Check string options in a buffer for NULL value.
  2042.  */
  2043.     void
  2044. check_buf_options(buf)
  2045.     BUF        *buf;
  2046. {
  2047.     if (buf->b_p_fo == NULL)
  2048.         buf->b_p_fo = empty_option;
  2049.     if (buf->b_p_isk == NULL)
  2050.         buf->b_p_isk = empty_option;
  2051.     if (buf->b_p_com == NULL)
  2052.         buf->b_p_com = empty_option;
  2053. #ifdef CINDENT
  2054.     if (buf->b_p_cink == NULL)
  2055.         buf->b_p_cink = empty_option;
  2056.     if (buf->b_p_cino == NULL)
  2057.         buf->b_p_cino = empty_option;
  2058. #endif
  2059. #if defined(SMARTINDENT) || defined(CINDENT)
  2060.     if (buf->b_p_cinw == NULL)
  2061.         buf->b_p_cinw = empty_option;
  2062. #endif
  2063. }
  2064.  
  2065. /*
  2066.  * Free the string allocated for an option.
  2067.  * Checks for the string being empty_option. This may happen if we're out of
  2068.  * memory, strsave() returned NULL, which was replaced by empty_option by
  2069.  * check_options().
  2070.  * Does NOT check for P_ALLOCED flag!
  2071.  */
  2072.     void
  2073. free_string_option(p)
  2074.     char_u        *p;
  2075. {
  2076.     if (p != empty_option)
  2077.         vim_free(p);
  2078. }
  2079.  
  2080. /*
  2081.  * Set a string option to a new value.
  2082.  * The string is copied into allocated memory.
  2083.  * If 'dofree' is set, the old value may be freed.
  2084.  * if (opt_idx == -1) name is used, otherwise opt_idx is used.
  2085.  */
  2086.     void
  2087. set_string_option(name, opt_idx, val, dofree)
  2088.     char_u    *name;
  2089.     int        opt_idx;
  2090.     char_u    *val;
  2091.     int        dofree;
  2092. {
  2093.     char_u    *s;
  2094.     char_u    **varp;
  2095.  
  2096.     if (opt_idx == -1)            /* use name */
  2097.     {
  2098.         opt_idx = findoption(name);
  2099.         if (opt_idx == -1)        /* not found (should not happen) */
  2100.             return;
  2101.     }
  2102.  
  2103.     if (options[opt_idx].var == NULL)    /* don't set hidden option */
  2104.         return;
  2105.  
  2106.     s = strsave(val);
  2107.     if (s != NULL)
  2108.     {
  2109.         varp = (char_u **)get_varp(&(options[opt_idx]));
  2110.         if (dofree && (options[opt_idx].flags & P_ALLOCED))
  2111.             free_string_option(*varp);
  2112.         *varp = s;
  2113.             /* if 'term' option set for the first time: set default value */
  2114.         if (varp == &(term_strings[KS_NAME]) &&
  2115.                                            *(options[opt_idx].def_val) == NUL)
  2116.         {
  2117.             options[opt_idx].def_val = s;
  2118.             options[opt_idx].flags |= P_DEF_ALLOCED;
  2119.         }
  2120.         else
  2121.             options[opt_idx].flags |= P_ALLOCED;
  2122.     }
  2123. }
  2124.  
  2125. /*
  2126.  * find index for option 'arg'
  2127.  * return -1 if not found
  2128.  */
  2129.     static int
  2130. findoption(arg)
  2131.     char_u *arg;
  2132. {
  2133.     int        opt_idx;
  2134.     char    *s;
  2135.  
  2136.     for (opt_idx = 0; (s = options[opt_idx].fullname) != NULL; opt_idx++)
  2137.     {
  2138.         if (STRCMP(arg, s) == 0) /* match full name */
  2139.             break;
  2140.     }
  2141.     if (s == NULL)
  2142.     {
  2143.         for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
  2144.         {
  2145.             s = options[opt_idx].shortname;
  2146.             if (s != NULL && STRCMP(arg, s) == 0) /* match short name */
  2147.                 break;
  2148.             s = NULL;
  2149.         }
  2150.     }
  2151.     if (s == NULL)
  2152.         opt_idx = -1;
  2153.     return opt_idx;
  2154. }
  2155.  
  2156.     char_u *
  2157. get_highlight_default()
  2158. {
  2159.     int i;
  2160.  
  2161.     i = findoption((char_u *)"hl");
  2162.     if (i >= 0)
  2163.         return options[i].def_val;
  2164.     return (char_u *)NULL;
  2165. }
  2166.  
  2167.     static int
  2168. find_key_option(arg)
  2169.     char_u *arg;
  2170. {
  2171.     int            key;
  2172.     int            c;
  2173.  
  2174.     /* don't use get_special_key_code() for t_xx, we don't want it to call
  2175.      * add_termcap_entry() */
  2176.     if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
  2177.         key = TERMCAP2KEY(arg[2], arg[3]);
  2178.  
  2179.     /* <S-Tab> is a special case, because TAB isn't a special key */
  2180.     else if (vim_strnicmp(arg, (char_u *)"S-Tab", (size_t)5) == 0)
  2181.         key = K_S_TAB;
  2182.     else
  2183.     {
  2184.         /* Currently only the shift modifier is recognized */
  2185.         mod_mask = 0;
  2186.         if (TO_LOWER(arg[0]) == 's' && arg[1] == '-')
  2187.         {
  2188.             mod_mask = MOD_MASK_SHIFT;
  2189.             arg += 2;
  2190.         }
  2191.         c = get_special_key_code(arg);
  2192.         key = check_shifted_spec_key(c);
  2193.         if (mod_mask && c == key)            /* key can't be shifted */
  2194.             key = 0;
  2195.     }
  2196.     return key;
  2197. }
  2198.  
  2199. /*
  2200.  * if 'all' == 0: show changed options
  2201.  * if 'all' == 1: show all normal options
  2202.  * if 'all' == 2: show all terminal options
  2203.  */
  2204.     static void
  2205. showoptions(all)
  2206.     int            all;
  2207. {
  2208.     struct option   *p;
  2209.     int                col;
  2210.     int                isterm;
  2211.     char_u            *varp;
  2212.     struct option    **items;
  2213.     int                item_count;
  2214.     int                run;
  2215.     int                row, rows;
  2216.     int                cols;
  2217.     int                i;
  2218.     int                len;
  2219.  
  2220. #define INC    20
  2221. #define GAP 3
  2222.  
  2223.     items = (struct option **)alloc((unsigned)(sizeof(struct option *) *
  2224.                                                                 PARAM_COUNT));
  2225.     if (items == NULL)
  2226.         return;
  2227.  
  2228.     set_highlight('t');        /* Highlight title */
  2229.     start_highlight();
  2230.     if (all == 2)
  2231.         MSG_OUTSTR("\n--- Terminal codes ---");
  2232.     else
  2233.         MSG_OUTSTR("\n--- Options ---");
  2234.     stop_highlight();
  2235.  
  2236.     /*
  2237.      * do the loop two times:
  2238.      * 1. display the short items
  2239.      * 2. display the long items (only strings and numbers)
  2240.      */
  2241.     for (run = 1; run <= 2 && !got_int; ++run)
  2242.     {
  2243.         /*
  2244.          * collect the items in items[]
  2245.          */
  2246.         item_count = 0;
  2247.         for (p = &options[0]; p->fullname != NULL; p++)
  2248.         {
  2249.             isterm = istermoption(p);
  2250.             varp = get_varp(p);
  2251.             if (varp != NULL && (
  2252.                 (all == 2 && isterm) ||
  2253.                 (all == 1 && !isterm) ||
  2254.                 (all == 0 && option_changed(p))))
  2255.             {
  2256.                 if (p->flags & P_BOOL)
  2257.                     len = 1;            /* a toggle option fits always */
  2258.                 else
  2259.                 {
  2260.                     option_value2string(p);
  2261.                     len = STRLEN(p->fullname) + strsize(NameBuff) + 1;
  2262.                 }
  2263.                 if ((len <= INC - GAP && run == 1) ||
  2264.                                                 (len > INC - GAP && run == 2))
  2265.                     items[item_count++] = p;
  2266.             }
  2267.         }
  2268.  
  2269.         /*
  2270.          * display the items
  2271.          */
  2272.         if (run == 1)
  2273.         {
  2274.             cols = (Columns + GAP - 3) / INC;
  2275.             if (cols == 0)
  2276.                 cols = 1;
  2277.             rows = (item_count + cols - 1) / cols;
  2278.         }
  2279.         else    /* run == 2 */
  2280.             rows = item_count;
  2281.         for (row = 0; row < rows && !got_int; ++row)
  2282.         {
  2283.             msg_outchar('\n');                        /* go to next line */
  2284.             if (got_int)                            /* 'q' typed in more */
  2285.                 break;
  2286.             col = 0;
  2287.             for (i = row; i < item_count; i += rows)
  2288.             {
  2289.                 msg_pos(-1, col);                    /* make columns */
  2290.                 showoneopt(items[i]);
  2291.                 col += INC;
  2292.             }
  2293.             flushbuf();
  2294.             mch_breakcheck();
  2295.         }
  2296.     }
  2297.     vim_free(items);
  2298. }
  2299.  
  2300. /*
  2301.  * Return TRUE if option is different from the default value
  2302.  */
  2303.     static int
  2304. option_changed(p)
  2305.     struct option    *p;
  2306. {
  2307.     char_u    *varp;
  2308.  
  2309.     varp = get_varp(p);
  2310.     if (varp == NULL)
  2311.         return FALSE;    /* hidden option is never changed */
  2312.  
  2313.     if (p->flags & P_NUM)
  2314.         return (*(long *)varp != (long)p->def_val);
  2315.     if (p->flags & P_BOOL)
  2316.                         /* the cast to long is required for Manx C */
  2317.         return (*(int *)varp != (int)(long)p->def_val);
  2318.     /* P_STRING */
  2319.     return STRCMP(*(char_u **)varp, p->def_val);
  2320. }
  2321.  
  2322. /*
  2323.  * showoneopt: show the value of one option
  2324.  * must not be called with a hidden option!
  2325.  */
  2326.     static void
  2327. showoneopt(p)
  2328.     struct option *p;
  2329. {
  2330.     char_u            *varp;
  2331.  
  2332.     varp = get_varp(p);
  2333.  
  2334.     if ((p->flags & P_BOOL) && !*(int *)varp)
  2335.         MSG_OUTSTR("no");
  2336.     else
  2337.         MSG_OUTSTR("  ");
  2338.     MSG_OUTSTR(p->fullname);
  2339.     if (!(p->flags & P_BOOL))
  2340.     {
  2341.         msg_outchar('=');
  2342.         option_value2string(p);        /* put string of option value in NameBuff */
  2343.         msg_outtrans(NameBuff);
  2344.     }
  2345. }
  2346.  
  2347. /*
  2348.  * Write modified options as set command to a file.
  2349.  * Return FAIL on error, OK otherwise.
  2350.  */
  2351.     int
  2352. makeset(fd)
  2353.     FILE *fd;
  2354. {
  2355.     struct option    *p;
  2356.     char_u            *s;
  2357.     int                e;
  2358.     char_u            *varp;
  2359.  
  2360.     /*
  2361.      * The options that don't have a default (terminal name, columns, lines)
  2362.      * are never written. Terminal options are also not written.
  2363.      */
  2364.     for (p = &options[0]; !istermoption(p); p++)
  2365.         if (!(p->flags & P_NO_MKRC) && !istermoption(p) &&
  2366.                                                           (option_changed(p)))
  2367.         {
  2368.             varp = get_varp(p);
  2369.             if (p->flags & P_BOOL)
  2370.                 fprintf(fd, "set %s%s", *(int *)(varp) ? "" : "no",
  2371.                                                                  p->fullname);
  2372.             else if (p->flags & P_NUM)
  2373.                 fprintf(fd, "set %s=%ld", p->fullname, *(long *)(varp));
  2374.             else    /* P_STRING */
  2375.             {
  2376.                 fprintf(fd, "set %s=", p->fullname);
  2377.                 s = *(char_u **)(varp);
  2378.                 /* some characters have to be escaped with CTRL-V or
  2379.                  * backslash */
  2380.                 if (s != NULL && putescstr(fd, s, TRUE) == FAIL)
  2381.                     return FAIL;
  2382.             }
  2383. #ifdef USE_CRNL
  2384.             putc('\r', fd);
  2385. #endif
  2386.                 /*
  2387.                  * Only check error for this putc, should catch at least
  2388.                  * the "disk full" situation.
  2389.                  */
  2390.             e = putc('\n', fd);
  2391.             if (e < 0)
  2392.                 return FAIL;
  2393.         }
  2394.     return OK;
  2395. }
  2396.  
  2397. /*
  2398.  * Clear all the terminal options.
  2399.  * If the option has been allocated, free the memory.
  2400.  * Terminal options are never hidden or indirect.
  2401.  */
  2402.     void
  2403. clear_termoptions()
  2404. {
  2405.     struct option   *p;
  2406.  
  2407.     /*
  2408.      * Reset a few things before clearing the old options. This may cause
  2409.      * outputting a few things that the terminal doesn't understand, but the
  2410.      * screen will be cleared later, so this is OK.
  2411.      */
  2412. #ifdef USE_MOUSE
  2413.     mch_setmouse(FALSE);            /* switch mouse off */
  2414. #endif
  2415.     mch_restore_title(3);            /* restore window titles */
  2416. #ifdef WIN32
  2417.     /*
  2418.      * Check if this is allowed now.
  2419.      */
  2420.     if (can_end_termcap_mode(FALSE) == TRUE)
  2421. #endif
  2422.         stoptermcap();                    /* stop termcap mode */
  2423.  
  2424.     for (p = &options[0]; p->fullname != NULL; p++)
  2425.         if (istermoption(p))
  2426.         {
  2427.             if (p->flags & P_ALLOCED)
  2428.                 free_string_option(*(char_u **)(p->var));
  2429.             if (p->flags & P_DEF_ALLOCED)
  2430.                 free_string_option(p->def_val);
  2431.             *(char_u **)(p->var) = empty_option;
  2432.             p->def_val = empty_option;
  2433.             p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
  2434.         }
  2435.     clear_termcodes();
  2436. }
  2437.  
  2438. /*
  2439.  * Set the terminal option defaults to the current value.
  2440.  * Used after setting the terminal name.
  2441.  */
  2442.     void
  2443. set_term_defaults()
  2444. {
  2445.     struct option   *p;
  2446.  
  2447.     for (p = &options[0]; p->fullname != NULL; p++)
  2448.         if (istermoption(p) && p->def_val != *(char_u **)(p->var))
  2449.         {
  2450.             if (p->flags & P_DEF_ALLOCED)
  2451.             {
  2452.                 free_string_option(p->def_val);
  2453.                 p->flags &= ~P_DEF_ALLOCED;
  2454.             }
  2455.             p->def_val = *(char_u **)(p->var);
  2456.             if (p->flags & P_ALLOCED)
  2457.             {
  2458.                 p->flags |= P_DEF_ALLOCED;
  2459.                 p->flags &= ~P_ALLOCED;        /* don't free the value now */
  2460.             }
  2461.         }
  2462. }
  2463.  
  2464. /*
  2465.  * return TRUE if 'p' starts with 't_'
  2466.  */
  2467.     static int
  2468. istermoption(p)
  2469.     struct option *p;
  2470. {
  2471.     return (p->fullname[0] == 't' && p->fullname[1] == '_');
  2472. }
  2473.  
  2474. /*
  2475.  * Compute columns for ruler and shown command. 'sc_col' is also used to
  2476.  * decide what the maximum length of a message on the status line can be.
  2477.  * If there is a status line for the last window, 'sc_col' is independent
  2478.  * of 'ru_col'.
  2479.  */
  2480.  
  2481. #define COL_RULER 17        /* columns needed by ruler */
  2482.  
  2483.     void
  2484. comp_col()
  2485. {
  2486.     int last_has_status = (p_ls == 2 || (p_ls == 1 && firstwin != lastwin));
  2487.  
  2488.     sc_col = 0;
  2489.     ru_col = 0;
  2490.     if (p_ru)
  2491.     {
  2492.         ru_col = COL_RULER + 1;
  2493.                             /* no last status line, adjust sc_col */
  2494.         if (!last_has_status)
  2495.             sc_col = ru_col;
  2496.     }
  2497.     if (p_sc)
  2498.     {
  2499.         sc_col += SHOWCMD_COLS;
  2500.         if (!p_ru || last_has_status)        /* no need for separating space */
  2501.             ++sc_col;
  2502.     }
  2503.     sc_col = Columns - sc_col;
  2504.     ru_col = Columns - ru_col;
  2505.     if (sc_col <= 0)            /* screen too narrow, will become a mess */
  2506.         sc_col = 1;
  2507.     if (ru_col <= 0)
  2508.         ru_col = 1;
  2509. }
  2510.  
  2511.     static char_u *
  2512. get_varp(p)
  2513.     struct option    *p;
  2514. {
  2515.     if (!(p->flags & P_IND) || p->var == NULL)
  2516.         return p->var;
  2517.  
  2518.     switch ((long)(p->var))
  2519.     {
  2520.         case PV_LIST:    return (char_u *)&(curwin->w_p_list);
  2521.         case PV_NU:        return (char_u *)&(curwin->w_p_nu);
  2522. #ifdef RIGHTLEFT
  2523.         case PV_RL:     return (char_u *)&(curwin->w_p_rl);
  2524. #endif
  2525.         case PV_SCROLL:    return (char_u *)&(curwin->w_p_scroll);
  2526.         case PV_WRAP:    return (char_u *)&(curwin->w_p_wrap);
  2527.         case PV_LBR:    return (char_u *)&(curwin->w_p_lbr);
  2528.  
  2529.         case PV_AI:        return (char_u *)&(curbuf->b_p_ai);
  2530.         case PV_BIN:    return (char_u *)&(curbuf->b_p_bin);
  2531. #ifdef CINDENT
  2532.         case PV_CIN:    return (char_u *)&(curbuf->b_p_cin);
  2533.         case PV_CINK:    return (char_u *)&(curbuf->b_p_cink);
  2534.         case PV_CINO:    return (char_u *)&(curbuf->b_p_cino);
  2535. #endif
  2536. #if defined(SMARTINDENT) || defined(CINDENT)
  2537.         case PV_CINW:    return (char_u *)&(curbuf->b_p_cinw);
  2538. #endif
  2539.         case PV_COM:    return (char_u *)&(curbuf->b_p_com);
  2540.         case PV_EOL:    return (char_u *)&(curbuf->b_p_eol);
  2541.         case PV_ET:        return (char_u *)&(curbuf->b_p_et);
  2542.         case PV_FO:        return (char_u *)&(curbuf->b_p_fo);
  2543.         case PV_INF:    return (char_u *)&(curbuf->b_p_inf);
  2544.         case PV_ISK:    return (char_u *)&(curbuf->b_p_isk);
  2545.         case PV_LISP:    return (char_u *)&(curbuf->b_p_lisp);
  2546.         case PV_ML:        return (char_u *)&(curbuf->b_p_ml);
  2547.         case PV_MOD:    return (char_u *)&(curbuf->b_changed);
  2548.         case PV_RO:        return (char_u *)&(curbuf->b_p_ro);
  2549. #ifdef SMARTINDENT
  2550.         case PV_SI:        return (char_u *)&(curbuf->b_p_si);
  2551. #endif
  2552. #ifndef SHORT_FNAME
  2553.         case PV_SN:        return (char_u *)&(curbuf->b_p_sn);
  2554. #endif
  2555.         case PV_SW:        return (char_u *)&(curbuf->b_p_sw);
  2556.         case PV_TS:        return (char_u *)&(curbuf->b_p_ts);
  2557.         case PV_TW:        return (char_u *)&(curbuf->b_p_tw);
  2558.         case PV_TX:        return (char_u *)&(curbuf->b_p_tx);
  2559.         case PV_WM:        return (char_u *)&(curbuf->b_p_wm);
  2560.         default:        EMSG("get_varp ERROR");
  2561.     }
  2562.     /* always return a valid pointer to avoid a crash! */
  2563.     return (char_u *)&(curbuf->b_p_wm);
  2564. }
  2565.  
  2566. /*
  2567.  * Copy options from one window to another.
  2568.  * Used when creating a new window.
  2569.  * The 'scroll' option is not copied, because it depends on the window height.
  2570.  */
  2571.     void
  2572. win_copy_options(wp_from, wp_to)
  2573.     WIN        *wp_from;
  2574.     WIN        *wp_to;
  2575. {
  2576.     wp_to->w_p_list = wp_from->w_p_list;
  2577.     wp_to->w_p_nu = wp_from->w_p_nu;
  2578. #ifdef RIGHTLEFT
  2579.     wp_to->w_p_rl = wp_from->w_p_rl;
  2580. #endif
  2581.     wp_to->w_p_wrap = wp_from->w_p_wrap;
  2582.     wp_to->w_p_lbr = wp_from->w_p_lbr;
  2583. }
  2584.  
  2585. /*
  2586.  * Copy options from one buffer to another.
  2587.  * Used when creating a new buffer and when entering a buffer.
  2588.  * Only do this once for a new buffer, otherwise allocated memory for the
  2589.  * string option will be lost.
  2590.  * When "entering" is TRUE we will enter the bp_to buffer.
  2591.  */
  2592.     void
  2593. buf_copy_options(bp_from, bp_to, entering)
  2594.     BUF        *bp_from;
  2595.     BUF        *bp_to;
  2596.     int        entering;
  2597. {
  2598.     /*
  2599.      * Don't copy if one of the pointers is NULL or they are the same.
  2600.      */
  2601.     if (bp_from == NULL || bp_to == NULL || bp_from == bp_to)
  2602.         return;
  2603.  
  2604.     /*
  2605.      * Always copy when entering and 'cpo' contains 'S'.
  2606.      * Don't copy when already initialized.
  2607.      * Don't copy when 'cpo' contains 's' and not entering.
  2608.      */
  2609.     if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !entering) &&
  2610.             (bp_to->b_p_initialized ||
  2611.                         (!entering && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
  2612.     {
  2613.         check_buf_options(bp_to);        /* make sure we don't have NULLs */
  2614.         return;
  2615.     }
  2616.  
  2617.     /*
  2618.      * If already initialized, need to free the allocated strings.
  2619.      * Copy 'readonly' and 'textmode' only when not initialized.
  2620.      */
  2621.     if (bp_to->b_p_initialized)
  2622.     {
  2623.         free_string_option(bp_to->b_p_fo);
  2624.         free_string_option(bp_to->b_p_isk);
  2625.         free_string_option(bp_to->b_p_com);
  2626. #ifdef CINDENT
  2627.         free_string_option(bp_to->b_p_cink);
  2628.         free_string_option(bp_to->b_p_cino);
  2629. #endif
  2630. #if defined(CINDENT) || defined(SMARTINDENT)
  2631.         free_string_option(bp_to->b_p_cinw);
  2632. #endif
  2633.     }
  2634.     else
  2635.     {
  2636.         bp_to->b_p_ro = FALSE;                /* don't copy readonly */
  2637.         bp_to->b_p_tx = bp_from->b_p_tx;
  2638.         bp_to->b_p_tx_nobin = bp_from->b_p_tx_nobin;
  2639.     }
  2640.  
  2641.     bp_to->b_p_ai = bp_from->b_p_ai;
  2642.     bp_to->b_p_ai_save = bp_from->b_p_ai_save;
  2643.     bp_to->b_p_sw = bp_from->b_p_sw;
  2644.     bp_to->b_p_tw = bp_from->b_p_tw;
  2645.     bp_to->b_p_tw_save = bp_from->b_p_tw_save;
  2646.     bp_to->b_p_tw_nobin = bp_from->b_p_tw_nobin;
  2647.     bp_to->b_p_wm = bp_from->b_p_wm;
  2648.     bp_to->b_p_wm_save = bp_from->b_p_wm_save;
  2649.     bp_to->b_p_wm_nobin = bp_from->b_p_wm_nobin;
  2650.     bp_to->b_p_bin = bp_from->b_p_bin;
  2651.     bp_to->b_p_et = bp_from->b_p_et;
  2652.     bp_to->b_p_et_nobin = bp_from->b_p_et_nobin;
  2653.     bp_to->b_p_ml = bp_from->b_p_ml;
  2654.     bp_to->b_p_ml_nobin = bp_from->b_p_ml_nobin;
  2655.     bp_to->b_p_inf = bp_from->b_p_inf;
  2656. #ifndef SHORT_FNAME
  2657.     bp_to->b_p_sn = bp_from->b_p_sn;
  2658. #endif
  2659.     bp_to->b_p_com = strsave(bp_from->b_p_com);
  2660.     bp_to->b_p_fo = strsave(bp_from->b_p_fo);
  2661. #ifdef SMARTINDENT
  2662.     bp_to->b_p_si = bp_from->b_p_si;
  2663.     bp_to->b_p_si_save = bp_from->b_p_si_save;
  2664. #endif
  2665. #ifdef CINDENT
  2666.     bp_to->b_p_cin = bp_from->b_p_cin;
  2667.     bp_to->b_p_cin_save = bp_from->b_p_cin_save;
  2668.     bp_to->b_p_cink = strsave(bp_from->b_p_cink);
  2669.     bp_to->b_p_cino = strsave(bp_from->b_p_cino);
  2670. #endif
  2671. #if defined(SMARTINDENT) || defined(CINDENT)
  2672.     bp_to->b_p_cinw = strsave(bp_from->b_p_cinw);
  2673. #endif
  2674. #ifdef LISPINDENT
  2675.     bp_to->b_p_lisp = bp_from->b_p_lisp;
  2676.     bp_to->b_p_lisp_save = bp_from->b_p_lisp_save;
  2677. #endif
  2678.     bp_to->b_p_ta_nobin = bp_from->b_p_ta_nobin;
  2679.  
  2680.     /*
  2681.      * Don't copy the options set by do_help(), use the saved values
  2682.      */
  2683.     if (!keep_help_flag && bp_from->b_help && help_save_isk != NULL)
  2684.     {
  2685.         bp_to->b_p_isk = strsave(help_save_isk);
  2686.         if (bp_to->b_p_isk != NULL)
  2687.             init_chartab();
  2688.         bp_to->b_p_ts = help_save_ts;
  2689.         bp_to->b_help = FALSE;
  2690.     }
  2691.     else
  2692.     {
  2693.         bp_to->b_p_isk = strsave(bp_from->b_p_isk);
  2694.         vim_memmove(bp_to->b_chartab, bp_from->b_chartab, (size_t)256);
  2695.         bp_to->b_p_ts = bp_from->b_p_ts;
  2696.         bp_to->b_help = bp_from->b_help;
  2697.     }
  2698.     check_buf_options(bp_to);
  2699.  
  2700.     /*
  2701.      * Set the flag that indicates that the options have been ininitialized.
  2702.      * Avoids loosing allocated memory.
  2703.      */
  2704.     bp_to->b_p_initialized = TRUE;
  2705. }
  2706.  
  2707. static int expand_option_idx = -1;
  2708. static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
  2709.  
  2710.     void
  2711. set_context_in_set_cmd(arg)
  2712.     char_u *arg;
  2713. {
  2714.     int         nextchar;
  2715.     int         flags = 0;        /* init for GCC */
  2716.     int            opt_idx = 0;    /* init for GCC */
  2717.     char_u        *p;
  2718.     char_u        *after_blank = NULL;
  2719.     int            is_term_option = FALSE;
  2720.     int            key;
  2721.  
  2722.     expand_context = EXPAND_SETTINGS;
  2723.     if (*arg == NUL)
  2724.     {
  2725.         expand_pattern = arg;
  2726.         return;
  2727.     }
  2728.     p = arg + STRLEN(arg) - 1;
  2729.     if (*p == ' ' && *(p - 1) != '\\')
  2730.     {
  2731.         expand_pattern = p + 1;
  2732.         return;
  2733.     }
  2734.     while (p != arg && (*p != ' ' || *(p - 1) == '\\'))
  2735.     {
  2736.         /* remember possible start of file name to expand */
  2737.         if ((*p == ' ' || (*p == ',' && *(p - 1) != '\\')) &&
  2738.                                                           after_blank == NULL)
  2739.             after_blank = p + 1;
  2740.         p--;
  2741.     }
  2742.     if (p != arg)
  2743.         p++;
  2744.     if (STRNCMP(p, "no", (size_t) 2) == 0)
  2745.     {
  2746.         expand_context = EXPAND_BOOL_SETTINGS;
  2747.         p += 2;
  2748.     }
  2749.     if (STRNCMP(p, "inv", (size_t) 3) == 0)
  2750.     {
  2751.         expand_context = EXPAND_BOOL_SETTINGS;
  2752.         p += 3;
  2753.     }
  2754.     expand_pattern = arg = p;
  2755.     if (*arg == '<')
  2756.     {
  2757.         while (*p != '>')
  2758.             if (*p++ == NUL)        /* expand terminal option name */
  2759.                 return;
  2760.         key = get_special_key_code(arg + 1);
  2761.         if (key == 0)                /* unknown name */
  2762.         {
  2763.             expand_context = EXPAND_NOTHING;
  2764.             return;
  2765.         }
  2766.         nextchar = *++p;
  2767.         is_term_option = TRUE;
  2768.         expand_option_name[2] = KEY2TERMCAP0(key);
  2769.         expand_option_name[3] = KEY2TERMCAP1(key);
  2770.     }
  2771.     else
  2772.     {
  2773.         if (p[0] == 't' && p[1] == '_')
  2774.         {
  2775.             p += 2;
  2776.             if (*p != NUL)
  2777.                 ++p;
  2778.             if (*p == NUL)
  2779.                 return;            /* expand option name */
  2780.             nextchar = *++p;
  2781.             is_term_option = TRUE;
  2782.             expand_option_name[2] = p[-2];
  2783.             expand_option_name[3] = p[-1];
  2784.         }
  2785.         else
  2786.         {
  2787.             while (isalnum(*p) || *p == '_' || *p == '*')    /* Allow * wildcard */
  2788.                 p++;
  2789.             if (*p == NUL)
  2790.                 return;
  2791.             nextchar = *p;
  2792.             *p = NUL;
  2793.             opt_idx = findoption(arg);
  2794.             *p = nextchar;
  2795.             if (opt_idx == -1 || options[opt_idx].var == NULL)
  2796.             {
  2797.                 expand_context = EXPAND_NOTHING;
  2798.                 return;
  2799.             }
  2800.             flags = options[opt_idx].flags;
  2801.             if (flags & P_BOOL)
  2802.             {
  2803.                 expand_context = EXPAND_NOTHING;
  2804.                 return;
  2805.             }
  2806.         }
  2807.     }
  2808.     if ((nextchar != '=' && nextchar != ':')
  2809.                                     || expand_context == EXPAND_BOOL_SETTINGS)
  2810.     {
  2811.         expand_context = EXPAND_UNSUCCESSFUL;
  2812.         return;
  2813.     }
  2814.     if (expand_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
  2815.     {
  2816.         expand_context = EXPAND_OLD_SETTING;
  2817.         if (is_term_option)
  2818.             expand_option_idx = -1;
  2819.         else
  2820.             expand_option_idx = opt_idx;
  2821.         expand_pattern = p + 1;
  2822.         return;
  2823.     }
  2824.     expand_context = EXPAND_NOTHING;
  2825.     if (is_term_option || (flags & P_NUM))
  2826.         return;
  2827.     if (after_blank != NULL)
  2828.         expand_pattern = after_blank;
  2829.     else
  2830.         expand_pattern = p + 1;
  2831.     if (flags & P_EXPAND)
  2832.     {
  2833.         p = options[opt_idx].var;
  2834.         if (p == (char_u *)&p_bdir || p == (char_u *)&p_dir ||
  2835.                                                        p == (char_u *)&p_path)
  2836.             expand_context = EXPAND_DIRECTORIES;
  2837.         else
  2838.             expand_context = EXPAND_FILES;
  2839.     }
  2840.     return;
  2841. }
  2842.  
  2843.     int
  2844. ExpandSettings(prog, num_file, file)
  2845.     regexp        *prog;
  2846.     int            *num_file;
  2847.     char_u        ***file;
  2848. {
  2849.     int num_normal = 0;        /* Number of matching non-term-code settings */
  2850.     int num_term = 0;        /* Number of matching terminal code settings */
  2851.     int opt_idx;
  2852.     int match;
  2853.     int count = 0;
  2854.     char_u *str;
  2855.     int    loop;
  2856.     int is_term_opt;
  2857.     char_u    name_buf[MAX_KEY_NAME_LEN];
  2858.  
  2859.     /* do this loop twice:
  2860.      * loop == 0: count the number of matching options
  2861.      * loop == 1: copy the matching options into allocated memory
  2862.      */
  2863.     for (loop = 0; loop <= 1; ++loop)
  2864.     {
  2865.         if (expand_context != EXPAND_BOOL_SETTINGS)
  2866.         {
  2867.             if (vim_regexec(prog, (char_u *)"all", TRUE))
  2868.             {
  2869.                 if (loop == 0)
  2870.                     num_normal++;
  2871.                 else
  2872.                     (*file)[count++] = strsave((char_u *)"all");
  2873.             }
  2874.             if (vim_regexec(prog, (char_u *)"termcap", TRUE))
  2875.             {
  2876.                 if (loop == 0)
  2877.                     num_normal++;
  2878.                 else
  2879.                     (*file)[count++] = strsave((char_u *)"termcap");
  2880.             }
  2881.         }
  2882.         for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
  2883.                                                                     opt_idx++)
  2884.         {
  2885.             if (options[opt_idx].var == NULL)
  2886.                 continue;
  2887.             if (expand_context == EXPAND_BOOL_SETTINGS
  2888.               && !(options[opt_idx].flags & P_BOOL))
  2889.                 continue;
  2890.             is_term_opt = istermoption(&options[opt_idx]);
  2891.             if (is_term_opt && num_normal > 0)
  2892.                 continue;
  2893.             match = FALSE;
  2894.             if (vim_regexec(prog, str, TRUE) ||
  2895.                                         (options[opt_idx].shortname != NULL &&
  2896.                          vim_regexec(prog,
  2897.                                  (char_u *)options[opt_idx].shortname, TRUE)))
  2898.                 match = TRUE;
  2899.             else if (is_term_opt)
  2900.             {
  2901.                 name_buf[0] = '<';
  2902.                 name_buf[1] = 't';
  2903.                 name_buf[2] = '_';
  2904.                 name_buf[3] = str[2];
  2905.                 name_buf[4] = str[3];
  2906.                 name_buf[5] = '>';
  2907.                 name_buf[6] = NUL;
  2908.                 if (vim_regexec(prog, name_buf, TRUE))
  2909.                 {
  2910.                     match = TRUE;
  2911.                     str = name_buf;
  2912.                 }
  2913.             }
  2914.             if (match)
  2915.             {
  2916.                 if (loop == 0)
  2917.                 {
  2918.                     if (is_term_opt)
  2919.                         num_term++;
  2920.                     else
  2921.                         num_normal++;
  2922.                 }
  2923.                 else
  2924.                     (*file)[count++] = strsave(str);
  2925.             }
  2926.         }
  2927.         /*
  2928.          * Check terminal key codes, these are not in the option table
  2929.          */
  2930.         if (expand_context != EXPAND_BOOL_SETTINGS  && num_normal == 0)
  2931.         {
  2932.             for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
  2933.             {
  2934.                 if (!isprint(str[0]) || !isprint(str[1]))
  2935.                     continue;
  2936.  
  2937.                 name_buf[0] = 't';
  2938.                 name_buf[1] = '_';
  2939.                 name_buf[2] = str[0];
  2940.                 name_buf[3] = str[1];
  2941.                 name_buf[4] = NUL;
  2942.  
  2943.                 match = FALSE;
  2944.                 if (vim_regexec(prog, name_buf, TRUE))
  2945.                     match = TRUE;
  2946.                 else
  2947.                 {
  2948.                     name_buf[0] = '<';
  2949.                     name_buf[1] = 't';
  2950.                     name_buf[2] = '_';
  2951.                     name_buf[3] = str[0];
  2952.                     name_buf[4] = str[1];
  2953.                     name_buf[5] = '>';
  2954.                     name_buf[6] = NUL;
  2955.  
  2956.                     if (vim_regexec(prog, name_buf, TRUE))
  2957.                         match = TRUE;
  2958.                 }
  2959.                 if (match)
  2960.                 {
  2961.                     if (loop == 0)
  2962.                         num_term++;
  2963.                     else
  2964.                         (*file)[count++] = strsave(name_buf);
  2965.                 }
  2966.             }
  2967.             /*
  2968.              * Check special key names.
  2969.              */
  2970.             for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
  2971.             {
  2972.                 name_buf[0] = '<';
  2973.                 STRCPY(name_buf + 1, str);
  2974.                 STRCAT(name_buf, ">");
  2975.  
  2976.                 reg_ic = TRUE;                    /* ignore case here */
  2977.                 if (vim_regexec(prog, name_buf, TRUE))
  2978.                 {
  2979.                     if (loop == 0)
  2980.                         num_term++;
  2981.                     else
  2982.                         (*file)[count++] = strsave(name_buf);
  2983.                 }
  2984.             }
  2985.         }
  2986.         if (loop == 0)
  2987.         {
  2988.             if (num_normal > 0)
  2989.                 *num_file = num_normal;
  2990.             else if (num_term > 0)
  2991.                 *num_file = num_term;
  2992.             else
  2993.                 return OK;
  2994.             *file = (char_u **) alloc((unsigned)(*num_file * sizeof(char_u *)));
  2995.             if (*file == NULL)
  2996.             {
  2997.                 *file = (char_u **)"";
  2998.                 return FAIL;
  2999.             }
  3000.         }
  3001.     }
  3002.     return OK;
  3003. }
  3004.  
  3005.     int
  3006. ExpandOldSetting(num_file, file)
  3007.     int        *num_file;
  3008.     char_u    ***file;
  3009. {
  3010.     char_u    *var = NULL;        /* init for GCC */
  3011.     char_u    *buf;
  3012.  
  3013.     *num_file = 0;
  3014.     *file = (char_u **)alloc((unsigned)sizeof(char_u *));
  3015.     if (*file == NULL)
  3016.         return FAIL;
  3017.  
  3018.     /*
  3019.      * For a terminal key code epand_option_idx is < 0.
  3020.      */
  3021.     if (expand_option_idx < 0)
  3022.     {
  3023.         var = find_termcode(expand_option_name + 2);
  3024.         if (var == NULL)
  3025.             expand_option_idx = findoption(expand_option_name);
  3026.     }
  3027.  
  3028.     if (expand_option_idx >= 0)
  3029.     {
  3030.         /* put string of option value in NameBuff */
  3031.         option_value2string(&options[expand_option_idx]);
  3032.         var = NameBuff;
  3033.     }
  3034.     else if (var == NULL)
  3035.         var = (char_u *)"";
  3036.  
  3037.     /* A backslash is required before some characters */
  3038.     buf = strsave_escaped(var, escape_chars);
  3039.  
  3040.     if (buf == NULL)
  3041.     {
  3042.         vim_free(*file);
  3043.         *file = NULL;
  3044.         return FAIL;
  3045.     }
  3046.  
  3047.     *file[0] = buf;
  3048.     *num_file = 1;
  3049.     return OK;
  3050. }
  3051.  
  3052. /*
  3053.  * Get the value for the numeric or string option *op in a nice format into
  3054.  * NameBuff[].  Must not be called with a hidden option!
  3055.  */
  3056.     static void
  3057. option_value2string(op)
  3058.     struct option    *op;
  3059. {
  3060.     char_u    *varp;
  3061.  
  3062.     varp = get_varp(op);
  3063.     if (op->flags & P_NUM)
  3064.     {
  3065.         if ((long *)varp == &p_wc)
  3066.         {
  3067.             if (IS_SPECIAL(p_wc) || find_special_key_in_table((int)p_wc) >= 0)
  3068.                 STRCPY(NameBuff, get_special_key_name((int)p_wc, 0));
  3069.             else
  3070.                 STRCPY(NameBuff, transchar((int)p_wc));
  3071.         }
  3072.         else
  3073.             sprintf((char *)NameBuff, "%ld", *(long *)varp);
  3074.     }
  3075.     else    /* P_STRING */
  3076.     {
  3077.         varp = *(char_u **)(varp);
  3078.         if (varp == NULL)                    /* just in case */
  3079.             NameBuff[0] = NUL;
  3080.         else if (op->flags & P_EXPAND)
  3081.             home_replace(NULL, varp, NameBuff, MAXPATHL);
  3082.         else
  3083.             STRNCPY(NameBuff, varp, MAXPATHL);
  3084.     }
  3085. }
  3086.  
  3087. /*
  3088.  * Convert the given pattern "pat" which has shell style wildcards in it, into
  3089.  * a regular expression, and return the result.  If there is a directory path
  3090.  * separator to be matched, then TRUE is put in allow_directories, otherwise
  3091.  * FALSE is put there -- webb.
  3092.  */
  3093.     char_u *
  3094. file_pat_to_reg_pat(pat, pat_end, allow_directories)
  3095.     char_u    *pat;
  3096.     char_u    *pat_end;                /* first char after pattern */
  3097.     int        *allow_directories;        /* Result passed back out in here */
  3098. {
  3099.     int        size;
  3100.     char_u    *endp;
  3101.     char_u    *reg_pat;
  3102.     char_u    *p;
  3103.     int        i;
  3104.     int        nested = 0;
  3105.     int        add_dollar = TRUE;
  3106.  
  3107.     if (allow_directories != NULL)
  3108.         *allow_directories = FALSE;
  3109.  
  3110.     size = 2;                /* '^' at start, '$' at end */
  3111.     for (p = pat; p < pat_end; p++)
  3112.     {
  3113.         switch (*p)
  3114.         {
  3115.             case '*':
  3116.             case '.':
  3117.             case ',':
  3118.             case '{':
  3119.             case '}':
  3120.             case '~':
  3121. #ifdef BACKSLASH_IN_FILENAME
  3122.             case '\\':
  3123. #endif
  3124.                 size += 2;
  3125.                 break;
  3126.             default:
  3127.                 size++;
  3128.                 break;
  3129.         }
  3130.     }
  3131.     reg_pat = alloc(size + 1);
  3132.     if (reg_pat == NULL)
  3133.         return NULL;
  3134.     i = 0;
  3135.     if (pat[0] == '*')
  3136.         while (pat[0] == '*' && pat < pat_end - 1)
  3137.             pat++;
  3138.     else
  3139.         reg_pat[i++] = '^';
  3140.     endp = pat_end - 1;
  3141.     if (*endp == '*')
  3142.     {
  3143.         while (endp - pat > 0 && *endp == '*')
  3144.             endp--;
  3145.         add_dollar = FALSE;
  3146.     }
  3147.     for (p = pat; *p && nested >= 0 && p <= endp; p++)
  3148.     {
  3149.         switch (*p)
  3150.         {
  3151.             case '*':
  3152.                 reg_pat[i++] = '.';
  3153.                 reg_pat[i++] = '*';
  3154.                 break;
  3155.             case '.':
  3156.             case '~':
  3157.                 reg_pat[i++] = '\\';
  3158.                 reg_pat[i++] = *p;
  3159.                 break;
  3160.             case '?':
  3161.                 reg_pat[i++] = '.';
  3162.                 break;
  3163.             case '\\':
  3164.                 if (p[1] == NUL)
  3165.                     break;
  3166. #ifdef BACKSLASH_IN_FILENAME
  3167.                 /* translate "\x" to "\\x", "\*" to "\\.*", and "\?" to "\\." */
  3168.                 if (isfilechar(p[1]) || p[1] == '*' || p[1] == '?')
  3169.                 {
  3170.                     reg_pat[i++] = '\\';
  3171.                     reg_pat[i++] = '\\';
  3172.                     if (allow_directories != NULL)
  3173.                         *allow_directories = TRUE;
  3174.                     break;
  3175.                 }
  3176.                 ++p;
  3177. #else
  3178.                 if (*++p == '?')
  3179.                     reg_pat[i++] = '?';
  3180.                 else
  3181. #endif
  3182.                      if (*p == ',')
  3183.                     reg_pat[i++] = ',';
  3184.                 else
  3185.                 {
  3186.                     if (allow_directories != NULL && ispathsep(*p))
  3187.                         *allow_directories = TRUE;
  3188.                     reg_pat[i++] = '\\';
  3189.                     reg_pat[i++] = *p;
  3190.                 }
  3191.                 break;
  3192.             case '{':
  3193.                 reg_pat[i++] = '\\';
  3194.                 reg_pat[i++] = '(';
  3195.                 nested++;
  3196.                 break;
  3197.             case '}':
  3198.                 reg_pat[i++] = '\\';
  3199.                 reg_pat[i++] = ')';
  3200.                 --nested;
  3201.                 break;
  3202.             case ',':
  3203.                 if (nested)
  3204.                 {
  3205.                     reg_pat[i++] = '\\';
  3206.                     reg_pat[i++] = '|';
  3207.                 }
  3208.                 else
  3209.                     reg_pat[i++] = ',';
  3210.                 break;
  3211.             default:
  3212.                 if (allow_directories != NULL && ispathsep(*p))
  3213.                     *allow_directories = TRUE;
  3214.                 reg_pat[i++] = *p;
  3215.                 break;
  3216.         }
  3217.     }
  3218.     if (add_dollar)
  3219.         reg_pat[i++] = '$';
  3220.     reg_pat[i] = NUL;
  3221.     if (nested != 0)
  3222.     {
  3223.         if (nested < 0)
  3224.             EMSG("Missing {.");
  3225.         else
  3226.             EMSG("Missing }.");
  3227.         vim_free(reg_pat);
  3228.         reg_pat = NULL;
  3229.     }
  3230.     return reg_pat;
  3231. }
  3232.  
  3233. #ifdef AUTOCMD
  3234. /*
  3235.  * functions for automatic commands
  3236.  */
  3237.  
  3238. static void show_autocmd __ARGS((AutoPat *ap, int event));
  3239. static void del_autocmd __ARGS((AutoPat *ap));
  3240. static void del_autocmd_cmds __ARGS((AutoPat *ap));
  3241. static int event_name2nr __ARGS((char_u *start, char_u **end));
  3242. static char *event_nr2name __ARGS((int event));
  3243. static char_u *find_end_event __ARGS((char_u *arg));
  3244. static int do_autocmd_event __ARGS((int event, char_u *pat,
  3245.                                                      char_u *cmd, int force));
  3246.  
  3247.     static void
  3248. show_autocmd(ap, event)
  3249.     AutoPat    *ap;
  3250.     int        event;
  3251. {
  3252.     AutoCmd *ac;
  3253.  
  3254.     if (got_int)                /* "q" hit for "--more--" */
  3255.         return;
  3256.     msg_outchar('\n');
  3257.     if (got_int)                /* "q" hit for "--more--" */
  3258.         return;
  3259.     msg_outchar('\n');
  3260.     if (got_int)                /* "q" hit for "--more--" */
  3261.         return;
  3262.     MSG_OUTSTR(event_nr2name(event));
  3263.     MSG_OUTSTR("  ");
  3264.     msg_outstr(ap->pat);
  3265.     for (ac = ap->cmds; ac != NULL; ac = ac->next)
  3266.     {
  3267.         MSG_OUTSTR("\n    ");
  3268.         if (got_int)            /* hit "q" at "--more--" prompt */
  3269.             return;
  3270.         msg_outtrans(ac->cmd);
  3271.     }
  3272. }
  3273.  
  3274. /*
  3275.  * Delete an autocommand pattern.
  3276.  */
  3277.     static void
  3278. del_autocmd(ap)
  3279.     AutoPat    *ap;
  3280. {
  3281.     vim_free(ap->pat);
  3282.     vim_free(ap->reg_pat);
  3283.     del_autocmd_cmds(ap);
  3284.     vim_free(ap);
  3285. }
  3286.  
  3287. /*
  3288.  * Delete the commands from a pattern.
  3289.  */
  3290.     static void
  3291. del_autocmd_cmds(ap)
  3292.     AutoPat *ap;
  3293. {
  3294.     AutoCmd    *ac;
  3295.  
  3296.     while (ap->cmds != NULL)
  3297.     {
  3298.         ac = ap->cmds;
  3299.         ap->cmds = ac->next;
  3300.         vim_free(ac->cmd);
  3301.         vim_free(ac);
  3302.     }
  3303. }
  3304.  
  3305. /*
  3306.  * Return the event number for event name "start".
  3307.  * Return -1 if the event name was not found.
  3308.  * Return a pointer to the next event name in "end".
  3309.  */
  3310.     static int
  3311. event_name2nr(start, end)
  3312.     char_u    *start;
  3313.     char_u    **end;
  3314. {
  3315.     char_u        *p;
  3316.     int            i;
  3317.     int            len;
  3318.  
  3319.     /* the event name ends with end of line, a blank or a comma */
  3320.     for (p = start; *p && !vim_iswhite(*p) && *p != ','; ++p)
  3321.         ;
  3322.     for (i = 0; event_names[i].name != NULL; ++i)
  3323.     {
  3324.         len = strlen(event_names[i].name);
  3325.         if (len == p - start &&
  3326.                    vim_strnicmp((char_u *)event_names[i].name, (char_u *)start, (size_t)len) == 0)
  3327.             break;
  3328.     }
  3329.     if (*p == ',')
  3330.         ++p;
  3331.     *end = p;
  3332.     if (event_names[i].name == NULL)
  3333.         return -1;
  3334.     return event_names[i].event;
  3335. }
  3336.  
  3337. /*
  3338.  * Return the name for event "event".
  3339.  */
  3340.     static char *
  3341. event_nr2name(event)
  3342.     int        event;
  3343. {
  3344.     int        i;
  3345.  
  3346.     for (i = 0; event_names[i].name != NULL; ++i)
  3347.         if (event_names[i].event == event)
  3348.             return event_names[i].name;
  3349.     return "Unknown";
  3350. }
  3351.  
  3352. /*
  3353.  * Scan over the events.  "*" stands for all events.
  3354.  */
  3355.     static char_u *
  3356. find_end_event(arg)
  3357.     char_u    *arg;
  3358. {
  3359.     char_u     *pat;
  3360.     char_u    *p;
  3361.  
  3362.     if (*arg == '*')
  3363.     {
  3364.         if (arg[1] && !vim_iswhite(arg[1]))
  3365.         {
  3366.             EMSG2("Illegal character after *: %s", arg);
  3367.             return NULL;
  3368.         }
  3369.         pat = arg + 1;
  3370.     }
  3371.     else
  3372.     {
  3373.         for (pat = arg; *pat && !vim_iswhite(*pat); pat = p)
  3374.         {
  3375.             if (event_name2nr(pat, &p) < 0)
  3376.             {
  3377.                 EMSG2("No such event: %s", pat);
  3378.                 return NULL;
  3379.             }
  3380.         }
  3381.     }
  3382.     return pat;
  3383. }
  3384.  
  3385. /*
  3386.  * do_autocmd() -- implements the :autocmd command.  Can be used in the
  3387.  *    following ways:
  3388.  *
  3389.  * :autocmd <event> <pat> <cmd>        Add <cmd> to the list of commands that
  3390.  *                                    will be automatically executed for <event>
  3391.  *                                    when editing a file matching <pat>.
  3392.  * :autocmd <event> <pat>            Show the auto-commands associated with
  3393.  *                                    <event> and <pat>.
  3394.  * :autocmd    <event>                    Show the auto-commands associated with
  3395.  *                                    <event>.
  3396.  * :autocmd                            Show all auto-commands.
  3397.  * :autocmd! <event> <pat> <cmd>    Remove all auto-commands associated with
  3398.  *                                    <event> and <pat>, and add the command
  3399.  *                                    <cmd>.
  3400.  * :autocmd! <event> <pat>            Remove all auto-commands associated with
  3401.  *                                    <event> and <pat>.
  3402.  * :autocmd! <event>                Remove all auto-commands associated with
  3403.  *                                    <event>.
  3404.  * :autocmd!                        Remove ALL auto-commands.
  3405.  *
  3406.  *    Multiple events and patterns may be given separated by commas.  Here are
  3407.  *    some examples:
  3408.  * :autocmd bufread,bufenter *.c,*.h    set tw=0 smartindent noic
  3409.  * :autocmd bufleave          *            set tw=79 nosmartindent ic infercase
  3410.  *
  3411.  * :autocmd * *.c                show all autocommands for *.c files.
  3412.  */
  3413.     void
  3414. do_autocmd(arg, force)
  3415.     char_u    *arg;
  3416.     int        force;
  3417. {
  3418.     char_u    *pat;
  3419.     char_u    *cmd;
  3420.     int        event;
  3421.  
  3422.     /*
  3423.      * Don't change autocommands while executing one.
  3424.      */
  3425.     if (autocmd_busy)
  3426.         return;
  3427.  
  3428.     /*
  3429.      * Scan over the events.
  3430.      * If we find an illegal name, return here, don't do anything.
  3431.      */
  3432.     pat = find_end_event(arg);
  3433.     if (pat == NULL)
  3434.         return;
  3435.  
  3436.     /*
  3437.      * Scan over the pattern.  Put a NUL at the end.
  3438.      */
  3439.     pat = skipwhite(pat);
  3440.     cmd = pat;
  3441.     while (*cmd && (!vim_iswhite(*cmd) || cmd[-1] == '\\'))
  3442.         cmd++;
  3443.     if (*cmd)
  3444.         *cmd++ = NUL;
  3445.  
  3446.     /*
  3447.      * Find the start of the commands.
  3448.      */
  3449.     cmd = skipwhite(cmd);
  3450.  
  3451.     /*
  3452.      * Print header when showing autocommands.
  3453.      */
  3454.     if (!force && *cmd == NUL)
  3455.     {
  3456.         set_highlight('t');        /* Highlight title */
  3457.         start_highlight();
  3458.         MSG_OUTSTR("\n--- Auto-Commands ---");
  3459.         stop_highlight();
  3460.     }
  3461.  
  3462.     /*
  3463.      * Loop over the events.
  3464.      */
  3465.     if (*arg == '*' || *arg == NUL)
  3466.     {
  3467.         for (event = 0; event < NUM_EVENTS; ++event)
  3468.             if (do_autocmd_event(event, pat, cmd, force) == FAIL)
  3469.                 break;
  3470.     }
  3471.     else
  3472.     {
  3473.         while (*arg && !vim_iswhite(*arg))
  3474.             if (do_autocmd_event(event_name2nr(arg, &arg), pat,
  3475.                                                           cmd, force) == FAIL)
  3476.                 break;
  3477.     }
  3478. }
  3479.  
  3480. /*
  3481.  * do_autocmd() for one event.
  3482.  * If *pat == NUL do for all patterns.
  3483.  * If *cmd == NUL show entries.
  3484.  * If force == TRUE delete entries.
  3485.  */
  3486.     static int
  3487. do_autocmd_event(event, pat, cmd, force)
  3488.     int        event;
  3489.     char_u    *pat;
  3490.     char_u    *cmd;
  3491.     int        force;
  3492. {
  3493.     AutoPat        *ap;
  3494.     AutoPat        *ap2;
  3495.     AutoPat        **final_ap;
  3496.     AutoCmd        *ac;
  3497.     AutoCmd        **final_ac;
  3498.     int            nested;
  3499.     char_u        *endpat;
  3500.     int            len;
  3501.  
  3502.     /*
  3503.      * Show or delete all patterns for an event.
  3504.      */
  3505.     if (*pat == NUL)
  3506.     {
  3507.         for (ap = first_autopat[event]; ap != NULL; ap = ap2)
  3508.         {
  3509.             ap2 = ap->next;
  3510.             if (force)
  3511.                 del_autocmd(ap);
  3512.             else
  3513.                 show_autocmd(ap, event);
  3514.         }
  3515.         if (force)
  3516.             first_autopat[event] = NULL;
  3517.     }
  3518.  
  3519.     /*
  3520.      * Loop through all the specified patterns.
  3521.      */
  3522.     for ( ; *pat; pat = (*endpat == ',' ? endpat + 1 : endpat))
  3523.     {
  3524.         /*
  3525.          * Find end of the pattern.
  3526.          * Watch out for a comma in braces, like "*.\{obj,o\}".
  3527.          */
  3528.         nested = 0;
  3529.         for (endpat = pat;
  3530.                 *endpat && (*endpat != ',' || nested || endpat[-1] == '\\');
  3531.                 ++endpat)
  3532.         {
  3533.             if (*endpat == '{')
  3534.                 nested++;
  3535.             else if (*endpat == '}')
  3536.                 nested--;
  3537.         }
  3538.         if (pat == endpat)                /* ignore single comma */
  3539.             continue;
  3540.  
  3541.         /*
  3542.          * Find entry with same pattern.
  3543.          */
  3544.         final_ap = &first_autopat[event];
  3545.         for (ap = first_autopat[event]; ap != NULL; ap = *final_ap)
  3546.         {
  3547.             len = STRLEN(ap->pat);
  3548.             if (len == endpat - pat && STRNCMP(pat, ap->pat, len) == 0)
  3549.                 break;
  3550.             final_ap = &ap->next;
  3551.         }
  3552.  
  3553.         /*
  3554.          * Add a new pattern.
  3555.          * Show and delete are ignored if pattern is not found.
  3556.          */
  3557.         if (ap == NULL)
  3558.         {
  3559.             if (*cmd == NUL)
  3560.                 continue;
  3561.  
  3562.             /* Add the autocmd at the end of the list */
  3563.             ap = (AutoPat *)alloc((unsigned)sizeof(AutoPat));
  3564.             if (ap == NULL)
  3565.                 return FAIL;
  3566.             ap->pat = strnsave(pat, (int)(endpat - pat));
  3567.             if (ap->pat == NULL)
  3568.             {
  3569.                 vim_free(ap);
  3570.                 return FAIL;
  3571.             }
  3572.             ap->reg_pat = file_pat_to_reg_pat(pat, endpat,
  3573.                                                       &ap->allow_directories);
  3574.             if (ap->reg_pat == NULL)
  3575.             {
  3576.                 vim_free(ap->pat);
  3577.                 vim_free(ap);
  3578.                 return FAIL;
  3579.             }
  3580.             ap->cmds = NULL;
  3581.             *final_ap = ap;
  3582.             ap->next = NULL;
  3583.         }
  3584.  
  3585.         /*
  3586.          * Remove existing autocommands.
  3587.          * If not adding any new autocmd's for this pattern, delete the
  3588.          * pattern from the autopat list
  3589.          */
  3590.         else if (force)
  3591.         {
  3592.             del_autocmd_cmds(ap);
  3593.             if (*cmd == NUL)
  3594.             {
  3595.                 if (ap == first_autopat[event])
  3596.                     first_autopat[event] = ap->next;
  3597.                 else
  3598.                 {
  3599.                     for (ap2 = first_autopat[event];
  3600.                             ap2->next != ap;
  3601.                             ap2 = ap2->next)
  3602.                         ;
  3603.                     ap2->next = ap->next;
  3604.                 }
  3605.                 del_autocmd(ap);
  3606.             }
  3607.         }
  3608.  
  3609.         /*
  3610.          * Show autocmd's for this autopat
  3611.          */
  3612.         if (*cmd == NUL && !force)
  3613.         {
  3614.             show_autocmd(ap, event);
  3615.         }
  3616.  
  3617.         /*
  3618.          * Add the autocmd at the end if it's not already there.
  3619.          */
  3620.         else if (*cmd != NUL)
  3621.         {
  3622.             final_ac = &(ap->cmds);
  3623.             for (ac = ap->cmds;
  3624.                     ac != NULL && STRCMP(cmd, ac->cmd) != 0;
  3625.                     ac = ac->next)
  3626.                 final_ac = &ac->next;
  3627.             if (ac == NULL)
  3628.             {
  3629.                 ac = (AutoCmd *)alloc((unsigned)sizeof(AutoCmd));
  3630.                 if (ac == NULL)
  3631.                     return FAIL;
  3632.                 ac->cmd = strsave(cmd);
  3633.                 if (ac->cmd == NULL)
  3634.                 {
  3635.                     vim_free(ac);
  3636.                     return FAIL;
  3637.                 }
  3638.                 ac->next = NULL;
  3639.                 *final_ac = ac;
  3640.             }
  3641.         }
  3642.     }
  3643.     return OK;
  3644. }
  3645.  
  3646. /*
  3647.  * Implementation of ":doautocmd event [fname]".
  3648.  */
  3649.     void
  3650. do_doautocmd(arg)
  3651.     char_u        *arg;
  3652. {
  3653.     char_u        *fname;
  3654.     int            nothing_done = TRUE;
  3655.  
  3656.     if (*arg == '*')
  3657.     {
  3658.         EMSG("Can't execute autocommands for ALL events");
  3659.         return;
  3660.     }
  3661.  
  3662.     /*
  3663.      * Scan over the events.
  3664.      * If we find an illegal name, return here, don't do anything.
  3665.      */
  3666.     fname = find_end_event(arg);
  3667.     if (fname == NULL)
  3668.         return;
  3669.  
  3670.     fname = skipwhite(fname);
  3671.  
  3672.     /*
  3673.      * Loop over the events.
  3674.      */
  3675.     while (*arg && !vim_iswhite(*arg))
  3676.         if (apply_autocmds(event_name2nr(arg, &arg), fname, NULL))
  3677.             nothing_done = FALSE;
  3678.  
  3679.     if (nothing_done)
  3680.         MSG("No matching autocommands");
  3681. }
  3682.  
  3683. /*
  3684.  * Execute autocommands for "event" and file name "fname".
  3685.  * Return TRUE if some commands were executed.
  3686.  */
  3687.     int
  3688. apply_autocmds(event, fname, fname_io)
  3689.     int                event;
  3690.     char_u            *fname;        /* NULL or empty means use actual file name */
  3691.     char_u            *fname_io;    /* fname to use for "^Vf" on cmdline */
  3692. {
  3693.     struct regexp    *prog;
  3694.     char_u            *tail;
  3695.     AutoPat            *ap;
  3696.     AutoCmd            *ac;
  3697.     int                temp;
  3698.     int                save_changed = curbuf->b_changed;
  3699.     char_u            *save_name;
  3700.     char_u            *full_fname = NULL;
  3701.     int                retval = FALSE;
  3702.  
  3703.     if (autocmd_busy)            /* no nesting allowed */
  3704.         return retval;
  3705.  
  3706.         /* Don't redraw while doing auto commands. */
  3707.     temp = RedrawingDisabled;
  3708.     RedrawingDisabled = TRUE;
  3709.     save_name = sourcing_name;    /* may be called from .vimrc */
  3710.     autocmd_fname = fname_io;
  3711.  
  3712.     /*
  3713.      * While applying autocmds, we don't want to allow the commands
  3714.      * :doautocmd or :autocmd.
  3715.      */
  3716.     autocmd_busy = TRUE;
  3717.  
  3718.     /*
  3719.      * When the file name is NULL or empty, use the file name of the current
  3720.      * buffer.  Always use the full path of the file name to match with, in
  3721.      * case "allow_directories" is set.
  3722.      */
  3723.     if (fname == NULL || *fname == NUL)
  3724.     {
  3725.         fname = curbuf->b_filename;
  3726.         if (fname == NULL)
  3727.             fname = (char_u *)"";
  3728.     }
  3729.     else
  3730.     {
  3731.         full_fname = FullName_save(fname);
  3732.         fname = full_fname;
  3733.     }
  3734.  
  3735.     tail = gettail(fname);
  3736.  
  3737.     for (ap = first_autopat[event]; ap != NULL; ap = ap->next)
  3738.     {
  3739. #ifdef CASE_INSENSITIVE_FILENAME
  3740.         reg_ic = TRUE;        /* Always ignore case */
  3741. #else
  3742.         reg_ic = FALSE;        /* Don't ever ignore case */
  3743. #endif
  3744.         reg_magic = TRUE;    /* Always use magic */
  3745.         prog = vim_regcomp(ap->reg_pat);
  3746.  
  3747.         if (prog != NULL &&
  3748.             ((ap->allow_directories && vim_regexec(prog, fname, TRUE)) ||
  3749.             (!ap->allow_directories && vim_regexec(prog, tail, TRUE))))
  3750.         {
  3751.             sprintf((char *)IObuff, "%s Auto commands for \"%s\"",
  3752.                                        event_nr2name(event), (char *)ap->pat);
  3753.             sourcing_name = strsave(IObuff);
  3754.             for (ac = ap->cmds; ac != NULL; ac = ac->next)
  3755.             {
  3756.                 do_cmdline(ac->cmd, TRUE, TRUE);
  3757.                 retval = TRUE;
  3758.             }
  3759.             vim_free(sourcing_name);
  3760.         }
  3761.         vim_free(prog);
  3762.     }
  3763.     RedrawingDisabled = temp;
  3764.     autocmd_busy = FALSE;
  3765.     sourcing_name = save_name;
  3766.     autocmd_fname = NULL;
  3767.     vim_free(full_fname);
  3768.  
  3769.     /* Some events don't set or reset the Changed flag */
  3770.     if (event == EVENT_BUFREADPOST || event == EVENT_BUFWRITEPOST ||
  3771.                      event == EVENT_FILEAPPENDPOST || event == EVENT_VIMLEAVE)
  3772.         curbuf->b_changed = save_changed;
  3773.  
  3774.     return retval;
  3775. }
  3776.  
  3777.     char_u    *
  3778. set_context_in_autocmd(arg, doautocmd)
  3779.     char_u    *arg;
  3780.     int        doautocmd;        /* TRUE for :doautocmd, FALSE for :autocmd */
  3781. {
  3782.     char_u    *p;
  3783.  
  3784.     /* skip over event name */
  3785.     for (p = arg; *p && !vim_iswhite(*p); ++p)
  3786.         if (*p == ',')
  3787.             arg = p + 1;
  3788.     if (*p == NUL)
  3789.     {
  3790.         expand_context = EXPAND_EVENTS;        /* expand event name */
  3791.         expand_pattern = arg;
  3792.         return NULL;
  3793.     }
  3794.  
  3795.     /* skip over pattern */
  3796.     arg = skipwhite(p);
  3797.     while (*arg && (!vim_iswhite(*arg) || arg[-1] == '\\'))
  3798.         arg++;
  3799.     if (*arg)
  3800.         return arg;                            /* expand (next) command */
  3801.  
  3802.     if (doautocmd)
  3803.         expand_context = EXPAND_FILES;        /* expand file names */
  3804.     else
  3805.         expand_context = EXPAND_NOTHING;    /* pattern is not expanded */
  3806.     return NULL;
  3807. }
  3808.  
  3809.     int
  3810. ExpandEvents(prog, num_file, file)
  3811.     regexp        *prog;
  3812.     int            *num_file;
  3813.     char_u        ***file;
  3814. {
  3815.     int        i;
  3816.     int        count;
  3817.     int        round;
  3818.  
  3819.     /*
  3820.      * round == 1: Count the matches.
  3821.      * round == 2: Save the matches into the array.
  3822.      */
  3823.     for (round = 1; round <= 2; ++round)
  3824.     {
  3825.         count = 0;
  3826.         for (i = 0; event_names[i].name != NULL; i++)
  3827.             if (vim_regexec(prog, (char_u *)event_names[i].name, TRUE))
  3828.             {
  3829.                 if (round == 1)
  3830.                     count++;
  3831.                 else
  3832.                     (*file)[count++] = strsave((char_u *)event_names[i].name);
  3833.             }
  3834.         if (round == 1)
  3835.         {
  3836.             *num_file = count;
  3837.             if (count == 0 || (*file = (char_u **)
  3838.                          alloc((unsigned)(count * sizeof(char_u *)))) == NULL)
  3839.                 return FAIL;
  3840.         }
  3841.     }
  3842.     return OK;
  3843. }
  3844.  
  3845. #endif  /* AUTOCMD */
  3846.  
  3847. #ifdef HAVE_LANGMAP
  3848. /*
  3849.  * Any character has an equivalent character.  This is used for keyboards that
  3850.  * have a special language mode that sends characters above 128 (although
  3851.  * other characters can be translated too).
  3852.  */
  3853.  
  3854. /* 
  3855.  * char_u langmap_mapchar[256];
  3856.  * Normally maps each of the 128 upper chars to an <128 ascii char; used to
  3857.  * "translate" native lang chars in normal mode or some cases of
  3858.  * insert mode without having to tediously switch lang mode back&forth.
  3859.  */
  3860.  
  3861.     static void 
  3862. langmap_init()
  3863. {
  3864.     int i;
  3865.  
  3866.     for (i = 0; i < 256; i++)            /* we init with a-one-to one map */
  3867.         langmap_mapchar[i] = i;
  3868. }
  3869.  
  3870. /*
  3871.  * Called when langmap option is set; the language map can be
  3872.  * changed at any time!
  3873.  */
  3874.     static void
  3875. langmap_set()
  3876. {
  3877.     char_u    *p;
  3878.     char_u    *p2;
  3879.     int        from, to;
  3880.  
  3881.     langmap_init();                            /* back to one-to-one map first */
  3882.  
  3883.     for (p = p_langmap; p[0]; )
  3884.     {
  3885.         for (p2 = p; p2[0] && p2[0] != ',' && p2[0] != ';'; ++p2)
  3886.             if (p2[0] == '\\' && p2[1])
  3887.                 ++p2;
  3888.         if (p2[0] == ';')
  3889.             ++p2;            /* abcd;ABCD form, p2 points to A */
  3890.         else
  3891.             p2 = NULL;        /* aAbBcCdD form, p2 is NULL */
  3892.         while (p[0])
  3893.         {
  3894.             if (p[0] == '\\' && p[1])
  3895.                 ++p;
  3896.             from = p[0];
  3897.             if (p2 == NULL)
  3898.             {
  3899.                 if (p[1] == '\\')
  3900.                     ++p;
  3901.                 to = p[1];
  3902.             }
  3903.             else
  3904.             {
  3905.                 if (p2[0] == '\\')
  3906.                     ++p2;
  3907.                 to = p2[0];
  3908.             }
  3909.             if (to == NUL)
  3910.             {
  3911.                 EMSG2("'langmap': Matching character missing for %s",
  3912.                                                              transchar(from));
  3913.                 return;
  3914.             }
  3915.             langmap_mapchar[from] = to;
  3916.  
  3917.             /* Advance to next pair */
  3918.             if (p2 == NULL)
  3919.             {
  3920.                 p += 2;
  3921.                 if (p[0] == ',')
  3922.                 {
  3923.                     ++p;
  3924.                     break;
  3925.                 }
  3926.             }
  3927.             else
  3928.             {
  3929.                 ++p;
  3930.                 ++p2;
  3931.                 if (*p == ';')
  3932.                 {
  3933.                     p = p2;
  3934.                     if (p[0])
  3935.                     {
  3936.                         if (p[0] != ',')
  3937.                         {
  3938.                             EMSG2("'langmap': Extra characters after semicolon: %s", p);
  3939.                             return;
  3940.                         }
  3941.                         ++p;
  3942.                     }
  3943.                     break;
  3944.                 }
  3945.             }
  3946.         }
  3947.     }
  3948. }
  3949. #endif
  3950.  
  3951. /*
  3952.  * Return TRUE if format option 'x' is in effect.
  3953.  * Take care of no formatting when 'paste' is set.
  3954.  */
  3955.     int
  3956. has_format_option(x)
  3957.     int        x;
  3958. {
  3959.     if (p_paste)
  3960.         return FALSE;
  3961.     return (vim_strchr(curbuf->b_p_fo, x) != NULL);
  3962. }
  3963.  
  3964. /*
  3965.  * Return TRUE if "x" is present in 'shortmess' option, or
  3966.  * 'shortmess' contains 'a' and "x" is present in SHM_A.
  3967.  */
  3968.     int
  3969. shortmess(x)
  3970.     int        x;
  3971. {
  3972.     return (vim_strchr(p_shm, x) != NULL || (vim_strchr(p_shm, 'a') != NULL &&
  3973.                                        vim_strchr((char_u *)SHM_A, x) != NULL));
  3974. }
  3975.  
  3976. /*
  3977.  * set_paste_option() - Called after p_paste was set or reset.
  3978.  */
  3979.     static void
  3980. paste_option_changed()
  3981. {
  3982.     static int        old_p_paste = FALSE;
  3983.     static int        save_sm = 0;
  3984.     static int        save_ru = 0;
  3985. #ifdef RIGHTLEFT
  3986.     static int        save_ri = 0;
  3987.     static int        save_hkmap = 0;
  3988. #endif
  3989.     BUF                *buf;
  3990.  
  3991.     if (p_paste)
  3992.     {
  3993.         /*
  3994.          * Paste switched from off to on.
  3995.          * Save the current values, so they can be restored later.
  3996.          */
  3997.         if (!old_p_paste)
  3998.         {
  3999.             /* save options for each buffer */
  4000.             for (buf = firstbuf; buf != NULL; buf = buf->b_next)
  4001.             {
  4002.                 buf->b_p_tw_save = buf->b_p_tw;
  4003.                 buf->b_p_wm_save = buf->b_p_wm;
  4004.                 buf->b_p_ai_save = buf->b_p_ai;
  4005. #ifdef SMARTINDENT
  4006.                 buf->b_p_si_save = buf->b_p_si;
  4007. #endif
  4008. #ifdef CINDENT
  4009.                 buf->b_p_cin_save = buf->b_p_cin;
  4010. #endif
  4011. #ifdef LISPINDENT
  4012.                 buf->b_p_lisp_save = buf->b_p_lisp;
  4013. #endif
  4014.             }
  4015.  
  4016.             /* save global options */
  4017.             save_sm = p_sm;
  4018.             save_ru = p_ru;
  4019. #ifdef RIGHTLEFT
  4020.             save_ri = p_ri;
  4021.             save_hkmap = p_hkmap;
  4022. #endif
  4023.         }
  4024.  
  4025.         /*
  4026.          * Always set the option values, also when 'paste' is set when it is
  4027.          * already on.
  4028.          */
  4029.         /* set options for each buffer */
  4030.         for (buf = firstbuf; buf != NULL; buf = buf->b_next)
  4031.         {
  4032.             buf->b_p_tw = 0;        /* textwidth is 0 */
  4033.             buf->b_p_wm = 0;        /* wrapmargin is 0 */
  4034.             buf->b_p_ai = 0;        /* no auto-indent */
  4035. #ifdef SMARTINDENT
  4036.             buf->b_p_si = 0;        /* no smart-indent */
  4037. #endif
  4038. #ifdef CINDENT
  4039.             buf->b_p_cin = 0;        /* no c indenting */
  4040. #endif
  4041. #ifdef LISPINDENT
  4042.             buf->b_p_lisp = 0;        /* no lisp indenting */
  4043. #endif
  4044.         }
  4045.  
  4046.         /* set global options */
  4047.         p_sm = 0;                    /* no showmatch */
  4048.         p_ru = 0;                    /* no ruler */
  4049. #ifdef RIGHTLEFT
  4050.         p_ri = 0;                    /* no reverse insert */
  4051.         p_hkmap = 0;                /* no Hebrew keyboard */
  4052. #endif
  4053.     }
  4054.  
  4055.     /*
  4056.      * Paste switched from on to off: Restore saved values.
  4057.      */
  4058.     else if (old_p_paste)
  4059.     {
  4060.         /* restore options for each buffer */
  4061.         for (buf = firstbuf; buf != NULL; buf = buf->b_next)
  4062.         {
  4063.             buf->b_p_tw = buf->b_p_tw_save;
  4064.             buf->b_p_wm = buf->b_p_wm_save;
  4065.             buf->b_p_ai = buf->b_p_ai_save;
  4066. #ifdef SMARTINDENT
  4067.             buf->b_p_si = buf->b_p_si_save;
  4068. #endif
  4069. #ifdef CINDENT
  4070.             buf->b_p_cin = buf->b_p_cin_save;
  4071. #endif
  4072. #ifdef LISPINDENT
  4073.             buf->b_p_lisp = buf->b_p_lisp_save;
  4074. #endif
  4075.         }
  4076.  
  4077.         /* restore global options */
  4078.         p_sm = save_sm;
  4079.         p_ru = save_ru;
  4080. #ifdef RIGHTLEFT
  4081.         p_ri = save_ri;
  4082.         p_hkmap = save_hkmap;
  4083. #endif
  4084.     }
  4085.  
  4086.     old_p_paste = p_paste;
  4087. }
  4088.  
  4089. /*
  4090.  * p_compatible_set() - Called when p_cp has been set.
  4091.  */
  4092.     static void
  4093. p_compatible_set()
  4094. {
  4095.     p_bs = 0;                        /* normal backspace */
  4096.                                     /* backspace and space do not wrap */
  4097.     set_string_option((char_u *)"ww", -1, (char_u *)"", TRUE);
  4098.     p_bk = 0;                        /* no backup file */
  4099.                     /* Use textwidth for formatting, don't format comments */
  4100.     set_string_option((char_u *)"fo", -1, (char_u *)FO_DFLT_VI, TRUE);
  4101.                                     /* all compatible flags on */
  4102.     set_string_option((char_u *)"cpo", -1, (char_u *)CPO_ALL, TRUE);
  4103.     set_string_option((char_u *)"isk", -1, (char_u *)"@,48-57,_", TRUE);
  4104.                                     /* no short messages */
  4105.     set_string_option((char_u *)"shm", -1, (char_u *)"", TRUE);
  4106. #ifdef DIGRAPHS
  4107.     p_dg = 0;                        /* no digraphs */
  4108. #endif /* DIGRAPHS */
  4109.     p_ek = 0;                        /* no ESC keys in insert mode */
  4110.     curbuf->b_p_et = 0;                /* no expansion of tabs */
  4111.     p_gd = 0;                        /* /g is not default for :s */
  4112.     p_hi = 0;                         /* no history */
  4113.     p_scs = 0;                        /* no ignore case switch */
  4114.     p_im = 0;                        /* do not start in insert mode */
  4115.     p_js = 1;                        /* insert 2 spaces after period */
  4116.     curbuf->b_p_ml = 0;                /* no modelines */
  4117.     p_more = 0;                        /* no -- more -- for listings */
  4118.     p_ru = 0;                        /* no ruler */
  4119. #ifdef RIGHTLEFT
  4120.     p_ri = 0;                        /* no reverse insert */
  4121.     p_hkmap = 0;                    /* no Hebrew keyboard mapping */
  4122. #endif
  4123.     p_sj = 1;                        /* no scrolljump */
  4124.     p_so = 0;                        /* no scrolloff */
  4125.     p_sr = 0;                        /* do not round indent to shiftwidth */
  4126.     p_sc = 0;                        /* no showcommand */
  4127.     p_smd = 0;                        /* no showmode */
  4128. #ifdef SMARTINDENT
  4129.     curbuf->b_p_si = 0;                /* no smartindent */
  4130. #endif
  4131. #ifdef CINDENT
  4132.     curbuf->b_p_cin = 0;            /* no C indenting */
  4133. #endif
  4134.     p_sta = 0;                        /* no smarttab */
  4135.     p_sol = TRUE;                    /* Move cursor to start-of-line */
  4136.     p_ta = 0;                        /* no automatic textmode detection */
  4137.     curbuf->b_p_tw = 0;                /* no automatic line wrap */
  4138.     p_to = 0;                        /* no tilde operator */
  4139.     p_ttimeout = 0;                    /* no terminal timeout */
  4140.     p_tr = 0;                        /* tag file names not relative */
  4141.     p_ul = 0;                        /* no multilevel undo */
  4142.     p_uc = 0;                        /* no autoscript file */
  4143.     p_wb = 0;                        /* no backup file */
  4144.     if (p_wc == TAB)
  4145.         p_wc = Ctrl('E');            /* normal use for TAB */
  4146.     init_chartab();                    /* make b_p_isk take effect */
  4147. }
  4148.  
  4149. /*
  4150.  * fill_breakat_flags() -- called when 'breakat' changes value.
  4151.  */
  4152.     static void
  4153. fill_breakat_flags()
  4154. {
  4155.     char_u        *c;
  4156.     int            i;
  4157.  
  4158.     for (i = 0; i < 256; i++) 
  4159.         breakat_flags[i] = FALSE;
  4160.  
  4161.     if (p_breakat != NULL)
  4162.         for (c = p_breakat; *c; c++) 
  4163.             breakat_flags[*c] = TRUE;
  4164. }
  4165.